OctoberCMS BackendForm-Widget "Repeater" stores my data as an array in the database which looks like so:
{
"topic":"title",
"topic_description":"description",
}
{
"topic":"title",
"topic_description":"description",
}
I need to add a unique ID to each array so the json looks like this:
{
"topic_id":"1",
"topic":"title",
"topic_description":"description",
}
{
"topic_id":"2",
"topic":"title",
"topic_description":"description",
}
I have added this formBeforeSave function to my controller:
public function formBeforeSave($model)
{
$model->topics = array_map(array($model, 'topics'), function ($topic) {
$topic['id'] = uniqid();
});
}
But when trying to save the form OctoberCMS throws this error:
“array_map(): Argument #2 should be an array”
Adding this to the controller instead:
public function formBeforeSave($model)
{
$model->topics = array_map($model->topics, function ($topic) {
$topic['id'] = uniqid();
});
}
Throws the error:
array_map() expects parameter 1 to be a valid callback, array must have exactly two members
Has anyone experienced this and found a solution he/she could share please?
Each topic has a (unique) key already. So you could just copy this key as additional value while walking through the array:
// mocking test-data:
// $model->topics = [["topic"=>"title"], ["bar"=>"foo"]];
array_walk($model->topics, function(&$topic, $key) {
$topic['topic_id'] = $key;
});
As per the docs first argument should be callback, replace your code with following and try if it works.
public function formBeforeSave($model)
{
$model->topics = array_map(function ($topic) {
$topic['id'] = uniqid();
return $topic;
}, $model->topics);
}
Reference: http://php.net/manual/en/function.array-map.php
Applaus goes to LukeTower who pointed me to the right direction on Github.
The solution is putting the logic in the beforeSave() method on the model:
public function beforeSave()
{
$this->_FieldName_ = array_map(function ($miau) {
$miau['id'] = uniqid();
return $miau;
}, $this->_FieldName_);
}
Related
I have this code:
public function taxesData(Product $product)
{
$taxes = \Auth::user()->taxes;
foreach ($taxes as $tax) {
echo "$product->getTax($tax)";
}
}
which on testing gives this error:
Type error: Too few arguments to function App\Product::getTax(), 0 passed in E:\projects\ims\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php on line 411 and exactly 1 expected
However, just a small change makes it works, but I am not able to understand. Why?
public function taxesData(Product $product)
{
$taxes = \Auth::user()->taxes;
foreach ($taxes as $tax) {
echo $product->getTax($tax);
}
}
Please help.
I tried to simplify it for the purpose of posting here... actually i am creating json with html component for a datatable ->
public function taxesData(Product $product)
{
$taxes = \Auth::user()->taxes;
return datatables()
->of($taxes)
->addColumn('check',function($tax) use($product){
if($product->hasTax($tax)){
return "<input type='checkbox' class='input-sm row-checkbox' name='tax[$tax->id]' value='$tax->id' checked>";
}else{
return "<input type='checkbox' class='input-sm row-checkbox' name='tax[$tax->id]' value='$tax->id'>";
}
})
->editColumn('tax', function($tax) use($product){
return "<span class='currencyinput form-control'>
<input id='rate' type='text' name='rate' value='$product->getTax($tax)' required autofocus>
</span>"
})
->toJson();
}
Adding getTax method
public function getTax(Tax $t)
{
if($this->hasTax($t)){
return $this->taxes->find($t->id)->pivot->tax;
}
else{
return $t->pivot->tax;
}
}
public function hasTax(Tax $tax)
{
foreach ($this->taxes as $t) {
if($t->id == $tax->id){
return true;
}
}
return false;
}
It fails because you are not following the correct syntax of echo strings.
This would work:
echo "{$product->getTax($tax)}";
or actually, because you dont' need the quotes for such a simple expression:
echo $product->getTax($tax);
Here's what I've done so far.
Just for simplicity, I've created a sample Model.
// SampleModel.php
public function relatedModels()
{
return $this->hasMany(RelatedModel::class);
}
// this is like an accessor, but since our model doesn't have
// a property called `relatedModels`, Laravel will ignore it
// until later...
public function getRelatedModels()
{
return "Sample";
}
Given the following code, here are the outputs.
$a = SampleModel::find($id);
$a->relatedModels;
// this returns a collection of related models to this model.
$a->getRelatedModels();
// this returns "Sample";
// HOWEVER, when we try to interpolate that member function call.
"$a->getRelatedModels()"
// this throws error that the method `getRelatedModels` must return a relationship.
// I've also tried to add an argument to my existing function to be in-line with your situation.
public function getRelatedModels($a) ...
// this works well
$a->getRelatedModels(1);
// but this, yes, it throws the error as same as what you've got.
"$a->getRelatedModels(1)";
The error pointed out this line in the framework's codebase.
// HasAttributes.php
protected function getRelationshipFromMethod($method)
{
$relation = $this->$method(); // <-- this line
For some reason, doing "$a->getRelatedModels(1)" triggers the __get magic method of the model.
Which branches down to this stack call.
// Model.php
public function __get($key)
{
return $this->getAttribute($key);
}
// |
// V
// HasAttributes.php
public function getAttribute($key)
{
...
return $this->getRelationValue($key);
}
// |
// V
// HasAttributes.php
public function getRelationValue($key)
{
...
if (method_exists($this, $key)) {
return $this->getRelationshipFromMethod($key);
}
}
// |
// V
// HasAttributes.php
protected function getRelationshipFromMethod($method)
{
$relation = $this->$method(); // <-- lastly to this
// $method = "getRelatedModels"
// since our method `getRelatedModels` needs an argument
// this call will fail since it wasn't able to provide an argument.
...
}
That's why you're getting the too few arguments passed exception. I want to investigate further but I have to go home!
I don't know if this is a legit bug for Laravel, but if you do think so, issue it on Laravel's github repository.
UPDATE
I've posted an issue in github and this is one of the comments which truly made sense for me.
This is neither a issue with Laravel, nor with PHP. You are just using the wrong syntax, see it here: https://github.com/laravel/framework/issues/23639
Github user #staudenmeir commented:
"$sampleModel->getRelatedModels()" is equivalent to "$sampleModel->getRelatedModels"."()".
The usage of variables in strings is limited to "$foo" and "$foo->bar". Function calls like "$foo->bar()"
don't work. You can (but shouldn't) use curly braces for that: "{$foo->bar()}"
The better solution is just simple string concatenation:
"text..." . $sampleModel->getRelatedModels() . "more text..."
So that is why the magic method __get is being called.
I have situation where codeigniter shows database Error Number 1048. It seems Values NULL but when I try to check it usign var_dump($_POST) Values are not NULL.
Controller : Jurusan.php
public function simpan()
{
$this->form_validation->set_rules('code','Kode','required|integer');
$this->form_validation->set_rules('jurusan','Jurusan','required');
$this->form_validation->set_rules('singkatan','Singkatan','required');
$this->form_validation->set_rules('ketua','Ketua','required');
$this->form_validation->set_rules('nik','NIK','required|integer');
$this->form_validation->set_rules('akreditasi','Akreditasi','required');
if($this->form_validation->run() == FALSE)
{
$isi['content'] = 'jurusan/form_tambahjurusan';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Tambah Jurusan';
$this->load->view('tampilan_home',$isi);
} else {
$this->model_security->getSecurity();
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code'];
$data['prodi'] = $this->input->post['jurusan'];
$data['singkat'] = $this->input->post['singkatan'];
$data['ketua_prodi'] = $this->input->post['ketua'];
$data['nik'] = $this->input->post['nik'];
$data['akreditasi'] = $this->input->post['akreditasi'];
$this->load->model('model_jurusan');
$query = $this->model_jurusan->getdata($key);
if($query->num_rows()>0)
{
$this->model_jurusan->getupdate($key,$data);
} else {
$this->model_jurusan->getinsert($data);
}
redirect('jurusan');
}
}
Model : model_jurusan.php
class Model_jurusan extends CI_model {
public function getdata($key)
{
$this->db->where('kd_prodi',$key);
$hasil = $this->db->get('prodi');
return $hasil;
}
public function getupdate($key,$data)
{
$this->db->where('kd_prodi',$key);
$this->db->update('prodi',$data);
}
public function getinsert($data)
{
$this->db->insert('prodi',$data);
}
}
Here is the error shown :
Here is the database structure :
You have a wrong syntax in these lines:
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code']; // <-- use ('code')
$data['prodi'] = $this->input->post['jurusan']; // <-- use ('jurusan')
Change this to
$this->input->post['array_key'];
this
$this->input->post('array_key');
Read : Input Class in Codeigniter
Well the problem lies in your way of accepting input parameters.
$this->input->post
is a method which accepts the variable name, not an array. So all the input parameters need to be passed as a function parameter to post method. These lines need to be altered to.
$data['kd_prodi'] = $this->input->post('code');
$data['prodi'] = $this->input->post('jurusan');
$data['singkat'] = $this->input->post('singkatan');
$data['ketua_prodi'] = $this->input->post('ketua');
$data['nik'] = $this->input->post('nik');
$data['akreditasi'] = $this->input->post('akreditasi');
Hope this solves the problem.
EDIT:
You did a var_dump($_POST) which works as it is supposed to and it will read the values of the post parameters. So either you fetch the parameters from $_POST array, or you use the $this->input->post() method. But I would suggest using the $this->input->post() method as it provides additional sanitization such as xss attack handling etc, which could be turned on an off from the config.
i have tried your code...it works. I think there some mistakes in your <input> tags, You must use <input name=""> not <input id=""> or something else. Hope it can help you out
You are try to get value from post is wrong. You should use at this way
$_POST['array value'];
I can't put real code here because is very long and will be hard to
explain.
I have users table in database and I have data table in database too.
So, to get the user data I'll pass user_id as parameter. Like this:
public function get_user_data($user_id) {
}
But. I can only get 1 data per "request". (Keep reading)
public function user_data() {
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
}
But, I guess that have an way to "bypass" this but I don't know. I'm having trouble thinking.
As I said, I want to "bypass" this, and be able to send multiple user IDs, my real function do not accept that by default and can't be changed.
Thanks in advance!
replace
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
to this
foreach($getUsers->result_array() as $user)
{
$data[] = $this->get_user_data($user->ID);
}
var_dump($data);
If you are aiming at sending more data to the function, you always need to make signature change of your function as one of the below :
function get_user_data() {
$args = func_get_args();
/** now you can access these as $args[0], $args[1] **/
}
Or
function get_user_data(...$user_ids) {
/** now you can access these as $user_ids[0], $user_ids[1] **/
}
// Only higher version of PHP
But I am not sure how you will handle returning data.
EDIT: Yes, then in the function, you can collect data in array and return an array of data from function.
If you can change in your function from where to where_in I think you will get an easy solution.
public function get_user_data($user_ids)
{
// your db code
$this->db->where_in('ID',$user_ids); //replace where with where_in
}
public function user_data()
{
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$user_ids[] = $user->ID;
}
$this->get_user_data($user_ids);
}
I'm not understanding why this is not working?
I am working in php and specifically laravel.
When I run it I get an undefined variable exception.
I would think I would get the array of bad_rows?
Also on a side question, how could I refactor this to be cleaner? Should I extract the Excel method to its own function?
I basically am trying to import the list, add it to the database and then I will redirect to the main page with the list of rows that were not imported by flashing it.
Thanks!
public function subscriberImportList(Request $request)
{
\Excel::filter('chunk')->load($request->file('import_list'))->chunk(100, function($rows) use($request)
{
$bad_rows = [];
foreach($rows as $row)
{
if (is_null($row->name) || is_null($row->street_address) || is_null($row->city)){
array_push($bad_rows, $row->name);
}
else {
//New Subscriber Instance
$subscriber = new Subscriber;
//Set the name
$subscriber->name = $row->name;
//Set the street address
$subscriber->street_address = $row->street_address;
//Set the city,state zip
preg_match('/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/', $row->city, $city_state_zip_seperated);
if (!$city_state_zip_seperated) {
array_push($bad_rows, $row->name);
}
else {
$subscriber->city = $city_state_zip_seperated[1];
$subscriber->state = $city_state_zip_seperated[2];
$subscriber->zipcode = $city_state_zip_seperated[3];
//Persist the subscriber to the database
$subscriber->save();
}
}
}
return $bad_rows;
});
dd($bad_rows);
}
You have:
public function subscriberImportList(Request $request) {
\Excel::[..snip..], function($rows) use($request) {
$bad_rows = [];
^^^^^^^^^^------defined here
blah blah blah
});
dd($bad_rows);
^^^^^^^^---used here
}
$bad_rows is only ever defined INSIDE that function($rows) closure, which means it's a local variable inside the closure, and exists nowhere else. So when you try dd($bad_rows), you are using an undefined variable.
What you should have is
public function .... {
$bad_rows = [];
^^^^^^^^^^^^^^^^
\Exce..... function($row) use($request, $bad_rows) {
^^^^^^^^^^^^
blah blah blah
});
dd($bad_rows);
}
I'm building a multilanguage website with Laravel4.
In the database i have column named "content" that contains serialized values for multiple languages. For example:
a:3:{s:2:"gb";s:15:"This is English";s:2:"es";s:5:"Hola!";s:2:"si";s:19:"Slovenija je zakon!";}
The serialized array contains of:
Language abbreviation, taken from Session
Content that comes from the input field
Now when I add new language to the database it creates new serialized string. Great!
But when I want to unserialize that string and add a value into it, i get the following error:
unserialize() [<a href='function.unserialize'>function.unserialize</a>]: Error at offset 0 of 30 bytes
Any ideas what is going on? I understand the meaning of the error, but it just makes no sense, since I'm sure that value in the database is serialized string.
public function setContentAttribute($value)
{
$lang = (Session::has('my.locale') ? Session::get('my.locale') : Config::get('app.locale'));
/* Create new serialized string */
if(empty($this->content)) {
$data[$lang] = $value['content'];
$this->attributes['content'] = serialize($data);
/* Update values */
} else {
$data = $this->content;
$data = unserialize($data)
$data[$lang] = $value['content'];
$this->attributes['content'] = serialize($data);
}
}
P.S: I'm using mutators for adding values to database.
I hope it's clear enough. If there is anything unclear, please comment and I'll fix it.
Thanks!
Ok, I've managed to fix it. I was unserializing my code twice - once in the accessor and once in the mutator. Here is a working example:
public function getVsebinaAttribute($value)
{
$data = unserialize($value);
$lang = $this->getLang();
if (!empty($data[$lang])) {
return $data[$lang];
} else {
return '# Value has not yet been added';
}
}
public function setVsebinaAttribute($value)
{
if (isset($this->attributes['vsebina'])) {
$data = unserialize($this->attributes['vsebina']);
} else {
$data = array();
}
$lang = $this->getLang();
$data[$lang] = $value;
$this->attributes['vsebina'] = serialize($data);
}
protected function getLang()
{
return Session::has('my.locale') ? Session::get('my.locale') : Config::get('app.locale');
}