When I fire this:
$this->model->find('all',array());
I get an array with the data of the model:
Array
(
[0] => Array
(
// some data
)
[1] => Array
(
// some data
)
[2] => Array
(
// some data
)
...
)
Now is it possible instead of 0,1,2 to get the id as key for each of the data?
For example:
Array
(
[365] => Array
(
[model] => Array
(
[id] => 365
// some data
)
)
[442] => Array
(
[model] => Array
(
[id] => 442
// some data
)
)
[1000] => Array
(
[model] => Array
(
[id] => 1000
// some data
)
)
...
)
I know that find('list') can do something like that but only for 2 fields max (one key and one value).
you can build it maybe this code can do it :
$final_array = array();
foreach($returned_array as $k => $v){
$final_array[$v['id']] = $v;
}
print_r($final_array);
It's still an after-find operation but take a look at Set::combine (http://book.cakephp.org/2.0/en/core-utility-libraries/set.html) as a more elegant solution. Something like:
$newArray = Set::combine($yourArray, '{n}.Model.id');
/* $newArray now looks like:
Array
(
[365] =>
[442] =>
[1000] =>
)
*/
(more or less just the the docs example) might work in your case.
Related
I am trying to using the in_array function to check the value exists in the second array or not. I want to search 556729685 from the following array:
$_SESSION["cart_item"] =
Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
I am using following functions to check but it is giving wrong out put:
in_array(556729685, array_keys($_SESSION["cart_item"]));
I have tried this one also:
in_array(556729685, array_values($_SESSION["cart_item"]));
None is working so help me to solve this issue.
Associative array consist of key, column and values. So, in order to check existence of value you need to reach key of array.
for($i=0;$i<count($_SESSION["cart_item"]);$i++)
{
if( in_array( 556729685 ,$_SESSION["cart_item"][$i] ) )
{
echo "exist in id:".$i.'<br/>';
}
}
Array looks like below :
Array
(
[0] => Array
(
[memberid] => 5203
)
[1] => Array
(
[memberid] => 494
)
[2] => Array
(
[memberid] => 1053
)
[3] => Array
(
[memberid] => 1081
)
)
How can i find-out the outer array index (such as 0,1,2) using inner array.
You can iterate the outer array, and save the key if the current inner array value matches like this:
$outerKey = null;
foreach($outerArray as $key => $member){
if($member['memberid'] == 1081){
$outerKey = $key;
break;
}
}
echo $outerKey; // it will print 3
How to save all data in cakePHP. Suppose I have number of records regarding single model I wish to save all data at a time. My array is in format.
Array
(
[Attendance] => Array
(
[0] => Array
(
[date] => 2013-10-09
[user_id] => 10
[attendance] => 1
)
[1] => Array
(
[date] => 2013-10-09
[user_id] => 8
[attendance] => 0
)
)
)
Is this possible to save all data a single time. I tried to do it using saveMany but it was not successful.
Do I need to do it in loop
foreach ($result as $data) {
$this->Attendance->create();
$this->request->data['Attendance'] = $data;
$this->Attendance->save($this->request->data);
}
Try saveMany instead of save
$this->Attendance->saveMany($this->request->data);
data should be in this format :
Array
(
[1] => Array
(
[Attendance] => Array
(
[field] => value
[field] => value
)
)
[2] => Array
(
[Attendance] => Array
(
[field_1] => value
[field_2] => value
)
)
)
CakePHP will save each record for each id you have on the array. The only requirement is to use the array structure convention by them, then use:
// Check method request
if ($this->request->is('post')) {
// Check not empty data
if (!empty($this->data)) {
// Save the data
if ($this-> Attendance->save($this->request->data)) {
// Set a session flash message and redirect.
$this->Session->setFlash('Attendance saved.');
$this->redirect('/Attendances');
} else {
$this->Session->setFlash('Error saving the data.');
}
}
}
I've got two tables that are setup to use the tree behavior, manufacturers, and categories.
Products can only belong to one category and only one manufacturer, however some manufacturers(child) are owned by other manufacturers(parent), and likewise some categories(child) are a subcategory of another(parent).
I want to do the following:
given a category id (parent), find all products in subcategories
given a manufacturer id (parent), find all products in child manufacturers
I have tried the following (in products controller):
$conditions['Product.category_id'] = $this->Product->Category->children($id,false,'id');
$this->paginate = array(
'conditions' => $conditions,
'limit' => 21
);
$products = $this->paginate('Product');
$this->set(compact('products'));
but it gives me this:
WHERE `Product`.`category_id` IN (Array, Array, Array, Array, Array, Array)
if I do a print_r I can see it is grabbing the information I need(see below), but how can I get to it, and is there better way to do this?
Array
(
[Product.category_id] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
)
)
[1] => Array
(
[Category] => Array
(
[id] => 12
)
)
[2] => Array
(
[Category] => Array
(
[id] => 23
)
)
[3] => Array
(
[Category] => Array
(
[id] => 24
)
)
[4] => Array
(
[Category] => Array
(
[id] => 25
)
)
[5] => Array
(
[Category] => Array
(
[id] => 26
)
)
)
)
The query is failing because it expects an array containing only the category-ids, like this:
$conditions['Product.category_id'] = array(1,4,5,6);
You can achieve this by 'extracting' those values from your array using Hash::extract() (or Set::extract() if you're using CakePHP 1.3)
$categoryIds = $this->Product->Category->children($id,false,'id');
$conditions['Product.category_id'] = Hash::extract($categoryIds, '{n}.Category.id);
Read the documentation on the Hash Utility here:
http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::extract
I have table project, screenshot, sub_screenshot, and user
I want to achieve this kind of diagram. Its like getting the 4th level.
[Model]
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 2
)
[Screenshot] = Array
(
[0] => Array
(
[Screenshot] => Array
(
[project_id] => 2
[id] => 1
)
[SubScreenshot] = Array
(
[0] => Array
(
[id] = 1
[project_id] = 2
[screenshot_id] = 1
)
[1] => Array
(
[id] = 2
[project_id] = 2
[screenshot_id] = 1
)
)
)
[1] => Array
(
[Screenshot] => Array
(
[project_id] => 3
[id] => 1
)
[SubScreenshot] = Array
(
[0] => Array
(
[id] = 1
[project_id] = 3
[screenshot_id] = 1
)
[1] => Array
(
[id] = 2
[project_id] = 3
[screenshot_id] = 1
)
[2] => Array
(
[id] = 2
[project_id] = 3
[screenshot_id] = 1
)
)
)
)
[User] => Array
(
[id] => 1
)
)
)
Regards,
Use the containable behavior. I've had some questions about it lately too. It would be much more efficient that setting recursive to 4 since you would only be including the models that you need.
Follow these instructions to setup the behavior on your models. http://book.cakephp.org/view/1323/Containable
Then, you should be able to perform a find and get the structure you want.
$this->Project->find(
'all',
array(
'conditions' => array(
//put your conditions in here
),
'contain' => array(
'User',
'Screenshot',
'Screenshot.SubScreenshot'
'Scrrenshot.SubScreenshot.User'
)
);
There are a few ways to use the model syntax. You can add arrays to the contained models to add conditions and/or further depth.
To give you some idea I suggest you to use the afterFind() callback
http://book.cakephp.org/view/1050/afterFind
After the find set the User wherever you want making another find with the User model.
I don't know if exists any configuration to modify the $recursive without a modification in the core.
EDIT
You can create a generic afterFind in AppModel when you can check if you have the $recursive=4 and copy the core functionality of the relationships and generate them.