A number appearing in place of first letter of array - cakephp-2.1

I am trying to create a upload plugin. I am keeping all the logic in behavior. This thing was working all fine by yesterday and from no where this strange problem is surfaced.
/*controller code */
debug($this->request->data);
$this->Model->saveAll($this->request->data);
/* outputs
Array
(
[Ad] => Array
(
[s] => 2
[d] => 2
)
[Upload] => Array
(
[field] => Upload
[table] => Ad
[filename] => Array
(
[name] => index.php
[type] => application/x-php
[tmp_name] => /tmp/php3MbvRh
[error] => 0
[size] => 32
)
)
)
*/
I am developing a plugin. In plugins beforeSave() i debug the same data and it shows
public function beforeSave(Model $Model) {
debug($Model->data);
}
/* outputs
Array
(
[Upload] => Array
(
[1pload] =>
)
)
*/
Everytime there appears a number for the fields property. Sometimes its 1, 8 and/or 9. :(
The plugin is loaded fine from bootstrap.php (CakePlugin::loadAll())
The plugin uses uploads table. The model of which is maintained as model.
The plugin behavior is properly defined and was working perfectly. NOT NOW

I couldn't figure out the problem. But the debug() in beforeSave() was being executed several times. So, I added in the plugin
if( isset ($this->data['preferred']['data'] ) {
}
For first few passes it is still something similar but on the third or fourth time it does show the data.
So, I have come with a theory that the beforeSave in plugin was being asynchronous to that of model's beforeSave and if i put a check then that would solve the issue. In fact it did.

Related

CodeIgniter 3.1.7 unbuffered_row() returning NULL but row() works

I am attempting to output a CSV from a query result. dbutil->csv_from_result() should work, but it only returns the column headers. No data is included.
I tracked it down to system/database/DB_utility.php which uses unbuffered_row().
Any calls to unbuffered_row() return NULL. If I change it to row(), I get a result. It makes no difference if I use unbuffered_row('array') or unbuffered_row('object')
Why does row() work but unbuffered_row() does not?
Is this a bug in CI or am I missing something?
Edit: row('array') doesn't seem to work either.
Edit: It seems that calling $query->result() spoils dbutil->csv_from_result($query). You apparently cannot iterate through query results AND then save the results in a CSV file. This was possible in CI 2.
Is there any way to show query results AND save the CSV without running the query twice?
I have faced similar problem and found this:
I have this query:
$query = $this->db->query("SELECT * FROM tablename");
print_r($query);
Then I get this and unbuffered_row works fine:
CI_DB_oci8_result Object
(
[stmt_id] => Resource id #106
[curs_id] =>
[limit_used] =>
[commit_mode] => 32
[conn_id] => Resource id #91
[result_id] => 1
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] =>
[row_data] =>
)
BUT if I call $query->num_rows() before I get a different CI_DB_oci8_result object and unbuffered_row didn't work returning null
$query = $this->db->query("SELECT * FROM tablename");
echo $query->num_rows();
print_r($query);
Hope this help somebody.

laravel 5.4 validation array with keys

In laravel 5.4 when validation is failed I do like:
if ($validator->fails()) {
$errors_list = $validator->messages()->all();
and I got array like :
[errors_list] => Array
(
[0] => The image has already been taken.
[1] => The is main has already been taken.
)
What I dislike in this output that actuall name of error field is ommitted.
Code
echo '<pre>$validator->messages()::'.print_r($validator->messages(),true).'</pre>';
has output:
$validator->messages()::Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[image] => Array
(
[0] => The image has already been taken.
)
[is_main] => Array
(
[0] => The is main has already been taken.
)
)
[format:protected] => :message
)
And I did not find how access to messages data.
I would like to get array like:
[errors_list] => Array
(
[image] => The image has already been taken.
[is_main] => The is main has already been taken.
)
Is there is a way to make it ?
Thanks!
I hope this will work for you.
$errors->has('image');
$errors->get(image);
$errors->has('is_main');
$errors->get(is_main);
It's a tricky one. The reason it's like that is because there may be multiple validation errors happening at once. However if you only have a single rule per entry:
array_combine($validator->messages()->keys(),$validator->messages()->all())

codeigniter form_validation is_unique compare data between tables

I have a from which lists several items that can be selected by check box and a dropdown permitter added. In a separate part of the form is have another check box. This is submitted as two arrays.
The arrays look like this
Array
(
[1] => Array
(
[num_copy] => 1
[dwg_rev] => B
[dwg_id] => 1
)
[2] => Array
(
[num_copy] => 1
[dwg_rev] => B
[dwg_id] => 2
)
)
Array
(
[1] => Array
(
[client_id] => 1
)
)
I need to pass these two arrays to a form_validation that can check is dwg_rev has already been added to the database for the selected client_id.
I tried to use is_unique, but it can only check in one table to see if this already exists in it.
This is what my form validation currently looks like
$rows = array();
if(!empty($_POST['result']))
{
$rows = $_POST['result'];
$temp_dwg_array = array_column($rows, 'temp_dwg_id');
foreach($temp_dwg_array as $key => $temp_dwg_id)
{
$this->form_validation->set_rules('result['.$temp_dwg_id.'][dwg_rev]', 'Revision' , 'required|callback_check_issued_rev');
}
} else $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Revision' , 'required');
if(!empty($_POST['client']))
{
$temp_client_array = array_column($_POST['client'],'client_id');
foreach($temp_client_array as $key => $client_id)
{
$this->form_validation->set_rules('client['.$client_id.'][client_id]', 'Please make a selection from the distribution list' , 'required|callback_check_issued_rev');
}
}
else $this->form_validation->set_rules('client[][client_id]', 'Distribution' , 'required');
I want to create a callback function, but I can't figure out how to pass two variables to the callback function to compare to the db.

getting the value of array key in codeigniter

I have the following line in my controller:
$data['faq'] = $this->faqModel->get();
This data print the following using the print_r
Array
(
[faq] => Array
(
[0] => Array
(
[faqid] => 12
[catid] => 122
[question] => How this CMS works
[question_en] => How this CMS works
[answer] => How this CMS works?
[answer_en] => How this CMS works?
[sorder] => 2
[visible] => 1
)
[1] => Array
(
[faqid] => 8
[catid] => 121
[question] => How does the design cost?
[question_en] => How does the design cost?
[answer] => How does the design cost?
[answer_en] => How does the design cost?
[sorder] => 1
[visible] => 1
)
)
)
I want to use the value stored in the [catid] key, and I am trying to do something like:
$data['faq']['catid'] to get that value in the controller (I want to make another select with that value) But I am getting with this error message: Undefined index: catid
Anyone can help me to get the value of ['catid']???
Regards, Zoran
Its 3 dimensional array u look closely there is two elements in faq array. You must wrote something like this: $data['faq'][0]['catid'] or $data['faq'][1]['catid']
The way you are accessing the array is incorrect, you are missing the item index on the second level. The correct way to use it as you are doing would be to do
echo $data['faq'][0]['faqid']; //faqid of the first item
However, this will only show one faqid at a time, and it not so useful when you are iterating. So, a good way would be this way.
foreach($data['faq'] as $value) {
echo $value['faqid'];
}

CodeIgniter: Using array within array

I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.

Resources