I want to save my array with multiple indexing array values.
Demo code
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I want to save this array values direct into table .Table name is audit_trail_details.
so please suggest mi proper solution for how to save this data into table.
You need to use saveAssociated and parse your array as the first parameter:-
$this->AuditTrailDetail->saveAssociated($data);
You will also need to make sure $data contains the AuditTrailDetail you are saving the associated data against. For example:-
Array
(
[AuditTrailDetail] => Array
(
[id] => 1
)
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
Related
In my cakephp Controller when I retrieve my data by find clause I get this array
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 1
[Category-name] => Arts
)
[Course] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[degree] => UG
[course-name] => BSc-Maths
)
[1] => Array
(
[id] => 5
[category_id] => 1
[degree] => PG
[course-name] => MSc Math
)
[2] => Array
(
[id] => 6
[category_id] => 1
[degree] => UG
[course-name] => Bsc Stats
)
[3] => Array
(
[id] => 7
[category_id] => 1
[degree] => PG
[course-name] => Msc-Stats
)
)
)
[1] => Array
(
[Category] => Array
(
[id] => 2
[Category-name] => Science and technology
)
[Course] => Array
(
[0] => Array
(
[id] => 2
[category_id] => 2
[degree] => UG
[course-name] => BSc-CS
)
)
)
[2] => Array
(
[Category] => Array
(
[id] => 3
[Category-name] => Commerce
)
[Course] => Array
(
[0] => Array
(
[id] => 3
[category_id] => 3
[degree] => PG
[course-name] => Msc-Finance
)
)
)
[3] => Array
(
[Category] => Array
(
[id] => 4
[Category-name] => Law
)
)
)
I want to show All Courses for a particular Category in ctp file as in form. As For Category Arts there are 4 Courses.
I want to display these 4 Courses for Arts Category.
I am able to Display Categories using the same array.
But not able to display Courses using same array in y ctp file as dropdown in a form.
Is there a way to Access this Courses data? Or Do I have to use different query for to access Courses??
Please I need your help with this.
Thanks in advance.
I would solve it like this:
An improved retrieving Query which only outputs the course names.
$cats = $this->Category->find('all',array(
'conditions'=>array('Category.Category-name'=>'Arts')
'recursive'=>1,
'contain'=>array('Course'),
'fields'=>'Course.course-name'))[0]['Course'];
Now the $cats variable should contain an array with 4 entries which can be used in a dropdown select
I have the following array and I would like to be able to organize it through collection
$collection = Collection($total);
$filter = $collection->groupBy('date_general')->toArray();
[2017-11-14] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
[1] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
Up to this point I already have the array organized by dates. Now within each date I need to organize it by user, that is to say that it is:
[2017-11-14] => Array
(
[164] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
)
[112] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
)
You'll have to process each date group and individually group their contents.
$collection = collection($total)
->groupBy('date_general')
->map(function ($data) {
return collection($data)->groupBy('user_id')->toArray();
});
See also
Cookbook > Collections
I have table name with TEST
id name_id name
1 11 one
2 11 two
3 12 three
4 12 four
5 13 five
i need out put like follows with single query without foreach loop.
array(
[11] => Array
(
[TEST] => Array
(
[name] => one
)
)
[11] => Array
(
[TEST] => Array
(
[name] => two
)
)
[12] => Array
(
[TEST] => Array
(
[name] => three
)
)
[12] => Array
(
[TEST] => Array
(
[name] => four
)
)
[13] => Array
(
[TEST] => Array
(
[name] => five
)
));
You can try putting the id with set::combine.
$data = set::combine(
$this->TEST->find('all', array('fields' => array('name'))),
'{n}.TEST.id',
'{n}.TEST'
);
A Car HABTM Image in the the table cars_images
Array
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
The car is being saved. The image is not (no record is being created, not in the images table nor in the cars_images table.
Thanks
You need a form looks like this:
echo $this->Form->input('Image.0.image_url');
echo $this->Form->input('Image.1.image_url');
echo $this->Form->input('Image.2.image_url');
So, produce this:
Array
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[0] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
[1] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
[2] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
)
And use saveAll:
$this->Car->saveAll($this->request->data);
You can find more information here.
For HABTM the array structure must(in your case)
(
[Car] => Array
(
[country_id] => 1
[body_type_id] => 7
[published] => 0
[variant] => 4 cyl AWD
)
[Image] => Array
(
[Image] => Array
(
[0] => Array
(
[image_url] => files/cars/subaru/outback/2012/Outback-3.6R3.jpg
)
)
)
)
And to "save the data" need to use
$this->Car->saveAll($data);
Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )