Strange value set as a key in the array in cakephp2 - php

I have the problem with the following code. The query sent was normal, just fetching all the data I need. However, When I print_r the $data, the all the datas from find('all') are not contained in [MYDATA] but also set in an unknown ['c'] and ['a'] array. Has anyone encounter this kind of weird problem ? Some examples or hint would be great. I would love to hear from you!
$data = $this->MYDATA->find(
'all',
array(
'conditions' =>
array(
array('my_id' => $var['id']),
),
'order' =>array('my_name'=>'DESC')
));
The following is the result from $data when using the 'my_name'=>'DESC' condition.
[0] => Array
(
[MYDATA] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
)
[c] => Array
(
[my_test] => math
)
[a] => Array
(
[my_date] => 2017-08-13
)
)
Without using the'my_name'=>'DESC' condition. This is the correct array. I want it to have the values return like this.
[0] => Array
(
[mydata] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[my_test] => math
[created] => 2017-06-19 15:39:44
[my_date] => 2017-08-13
)

If you want to get most of the benefits of Cakephp then must follow cakephp 2 conventions. As you mentioned in the comment that you model is MyData then it must be MyData not MyData while fetching data from the database. Please change MYDATA to MyData.
$data = $this->MyData->find(
'all',
array(
'conditions' =>array('my_id' => $var['id']),
'order' =>array('my_name'=>'DESC')
)
);
pr($data);
You can learn more about cakephp 2 conventions from here:
https://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

Related

openerp XML RPC search by many2one id

I'm currently working with XMLRPC API for connecting a website to openerp . The idea is we want to get the latest stock quantity from openerp. Currently i'm using this connector library . This is the sample data from my openerp api
[0] => Array
(
[create_date] => 2016-01-26 03:02:29
[qty] => 6
[propagated_from_id] =>
[package_id] =>
[cost] => 1500000
[inventory_value] => 9000000
[lot_id] =>
[reservation_id] =>
[id] => 2
[negative_dest_location_id] =>
[create_uid] => Array
(
[0] => 1
[1] => Administrator
)
[display_name] => 17326: 6.0Unit(s)
[__last_update] => 2016-01-26 03:02:29
[location_id] => Array
(
[0] => 19
[1] => Warehouse 1/Stock
)
[company_id] => Array
(
[0] => 1
[1] => PT. ONE WAY
)
[history_ids] => Array
(
[0] => 2
)
[owner_id] =>
[write_date] => 2016-01-26 03:02:29
[write_uid] => Array
(
[0] => 1
[1] => Administrator
)
[name] => 17326: 6.0Unit(s)
[product_id] => Array
(
[0] => 2756
[1] => [17326] AEG Vacuum Cleaner Dust Extractor Wet & Dry AP 20
)
[packaging_type_id] =>
[negative_move_id] =>
[in_date] => 2016-01-26 03:02:29
)
How can i filter that data by product_id
[product_id] => Array
(
[0] => 2756
[1] => [17326] AEG Vacuum Cleaner Dust Extractor Wet & Dry AP 20
)
Here is my code
$rpc = new OpenERP();
$x = $rpc->login("supermin", "my_site", "my_pass", "http://111.222.33.44:8069/xmlrpc/");
$data = $rpc->searchread(
array(
array("model", "=", "product.product"),
array("module", "=", "sale"),
array("product_id", "=", "2756"),
),
"stock.quant"
);
Any example will be helpful. Thanks
Try to use this:
//... your source
$data = $rpc->searchread(
array(
array('model', '=', 'product.product'),
array('module', '=', 'sale'),
array('product_id', '=', '2756'),
),
'stock.quant',
array(), // default
0, // default
10, // default
'product_id DESC' // default value was 'id DESC'
);
I did not use connector lib, but if we check searchread() method of OpenERP class we can see that default $order = "id DESC".
Hope this help you.

Retrieving data in unidimensional array in Cakephp 1.3

I am using cakephp 1.3 to extract data from a table with Cities and Postal Codes and I would like to extract these two columns to populate drop down menu with postal codes as menu 'id' and town names as menu 'values'.
The “find( 'list')” method is out of option because some of the towns have the same postcodes and the class is picking only unique value so instead of getting :
Array
(
[800] => Darwin
[801] => Darwin
[810] => Allawa
)
I am getting
Array
(
[800] => Darwin
[810] => Allawa
)
The method find('all') is returning a multidimensional array
Array
(
[0] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Acton
)
)
[1] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Ainslie
)
)
)
What is the best work around to get the desired data in unidimensional array without reducing the output to unique array keys? Something like :
Array ( [800] => Darwin [801] => Darwin [810] => Allawa )
You should still be able to do this with $Model->find('list').
$this->Model->find(
'list',
array(
'fields' => array(
'postal_code',
'city_name'
),
'contain' => false
)
);
This should return a list resultset using the two fields that you selected (postal code and city name).
Your alternate option, is to do a find('all'):
$results = $this->Model->find(
'all',
array(
'conditions' => array( ... )
)
);
Now, you have this format in your data results:
$results = Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 1
[postal_code] => 800,
[city_name] => 'Darwin'
)
)
[1] => Array
(
[ModelName] => Array
(
[id] => 2
[postal_code] => 801,
[city_name] => 'Darwin'
)
)
[2] => Array
(
[ModelName] => Array
(
[id] => 3
[postal_code] => 810,
[city_name] => 'Allawa'
)
)
)
Then use the Set class within Cake to extract some data:
$keys = Set::extract('/ModelName/postal_code', $results);
$values = Set::extract('/ModelName/city_name', $results);
/* Then some array_combine magic */
$list = array_combine($keys, $values);
Now you have an array that is what you want. Albeit a longer way, but it works. You can then pass this variable to your Form::select() helper to create your dropdown.
Hope this helps!

Remove key from multidimensional array in PHP

Hi I am going to make tree array for multilevel category menu and I have found the solution from below link:
stackoverflow answer
But my problem is that i am getting output of my array as FORMAT-1 and in the above given link the source array required as FORMAT-2
So can you please give me a hand for how to convert my array from FORMAT-1 to FORMAT-2
FORMAT-1
Array
(
[0] => Array
(
[category_id] => 11
[category_name] => Accessories
[parent_category_id] => 1
)
[1] => Array
(
[category_id] => 12
[category_name] => Keyrings
[parent_category_id] => 1
)
[2] => Array
(
[category_id] => 13
[category_name] => Photo Frames/Photo Albums
[parent_category_id] => 1
)
)
FORMAT-2 (I want output as below)
Array(
Array(
'category_id' => 11
'category_name' => 'Accessories'
'parent_category_id' => 1
),
Array(
'category_id' => 12
'category_name' => 'Keyrings'
'parent_category_id' => 1
),
Array(
'category_id' => 13
'category_name' => 'Photo Frames/Photo Albums'
'parent_category_id' => 1
)
);
Thanks in advance for your help and much appreciated
Both arrays are more are like same..
Format-1 looks print_r($yourarray); version
Format-2 looks like var_export($yourarray); version.
You need to have a look at the debugger functions.. print_r(), var_dump() and var_export().
If your purpose is debugging you may use <pre>var_dump()</pre>.

php - cakephp - Specify hasMany columns / fields in pagination with recursive

I have looked through containable component and other questions here, but doesn't look like that can help me.
Basically, I have paginate set to 1, which returns me something just like
Array
(
[0] => Array
(
[ModelName1] => Array
(
[id] => 83
[parent_id] => null
[field1] => value1
)
[AssociatedModelName1] => Array
(
[id] => 1
[field1] => value1
)
[children] => Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 42
[parent_id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 2
[field1] => value1
[field2] => value2
[field3] => value3
)
I am able to limit the output of ModelName1 and AssociatedModelName1 's field by adding
$fields => array("ModelName1.field1")
to the setting array. In fact I pass this to the "column" of the DataTable Plugin's component initialization, but it should be the same.
However, since there are SO MANY CHILDREN, and each children contains a lot of fields, I would like to get only the "field2" of each children.
How can I achieve that? If I specify "Children.ModelName1", I get a SQL1054 error, which is column not found.
Any clue?
You must use a conditional array in one way or another:
$this->paginate['Model'] = array(
'contain' => array(
'ChildModel' => array(
'fields' => array('ChildModel.field1', 'Childmodel.field2'),
'ChildChildModel' => array(
'fields' => array('ChildChildModel.field1', 'ChildChildModel.field2'),
)
)
)
);

PHP simple array manipulation for filtering common keys

I know it's simple task, but my mind not clicking at present for appropriate solution.
I have 2 arrays
$array1=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
)
$array2=Array
(
[302] => Array
(
[medication_id] => 302
[id] => 14
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
[change_date] =>
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
I want Output:
$outputarray=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
means if same key is exist in array1 then take it from array1 else from array2.
I tried with some PHP functions like array_merge,array_merge_recursive but not got desired output.
am not able to recollect memory now, Please suggest me any PHP array function to sort out this.
It's so simple that it's almost unethical:
$outputArray = $array1 + $array2;
Array union (above) does what you describe. Using array_merge could also work in general, but not directly with such input because your keys are integers.
Maybe this http://php.net/manual/pt_BR/function.array-intersect.php
Check with in_array() & store using array_push() will be a solution for you.

Categories