how to categories(group - classify) array by date - php

How can I print this array correctly. All the 27/Feb should be shown in Node(1) but one of then is goning to next node. This array has been grouped and DESC by date. Here is my array
$briefHistories = array(
0 => array(
'BriefHistory' => array(
'id' => '2',
'created' => '2014-02-28 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '1'
)
),
1 => array(
'BriefHistory' => array(
'id' => '4',
'created' => '2014-02-27 16:18:40',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '4'
)
),
2 => array(
'BriefHistory' => array(
'id' => '4',
'created' => '2014-02-27 16:40:50',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '4'
)
),
3 => array(
'BriefHistory' => array(
'id' => '1',
'created' => '2014-02-27 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '1'
)
),
4 => array(
'BriefHistory' => array(
'id' => '3',
'created' => '2014-02-26 16:18:09',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '3'
)
),
5 => array(
'BriefHistory' => array(
'id' => '3',
'created' => '2014-02-26 16:18:09',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '3'
)
)
);
//Php code
$bdate = $briefHistories[0]['BriefHistory']['created'];
$arrayDates = array(); $i = 0;$j=0;
foreach ($briefHistories as $briefHistorie) {
if (date('Y-m-d', strtotime($bdate))>date('Y-m-d', strtotime($briefHistorie['BriefHistory']['created']))){
$arrayDates[$i][$j] = $briefHistorie;
} else {
$arrayDates[$i][$j] = $briefHistorie;
$i++;
}
$bdate = date('Y-m-d', strtotime($bdate))>date('Y-m-d', strtotime($briefHistorie['BriefHistory']['created']))?$briefHistorie['BriefHistory']['created']:$bdate;
$j++;
}
debug($arrayDates);
output>
array(
(int) 0 => array(
(int) 0 => array(
'BriefHistory' => array(
'id' => '2',
'created' => '2014-02-28 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '1'
)
)
),
(int) 1 => array(
(int) 1 => array(
'BriefHistory' => array(
'id' => '4',
'created' => '2014-02-27 16:18:40',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '4'
)
),
(int) 2 => array(
'BriefHistory' => array(
'id' => '4',
'created' => '2014-02-27 16:40:50',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '4'
)
)
),
(int) 2 => array(
(int) 3 => array(
'BriefHistory' => array(
'id' => '1',
'created' => '2014-02-27 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '1'
)
)
),
(int) 3 => array(
(int) 4 => array(
'BriefHistory' => array(
'id' => '3',
'created' => '2014-02-26 16:18:09',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '3'
)
),
(int) 5 => array(
'BriefHistory' => array(
'id' => '3',
'created' => '2014-02-26 16:18:09',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '3'
)
)
)
)
I want to show it like this list
-----------
28/Feb/2014
-----------
27/Feb/2014
27/Feb/2014
27/Feb/2014
------------
26/Feb/2014
26/Feb/2014

Try this
$arrayDates = array();
$i = 0;
$key = array();
foreach ($briefHistories as $briefHistorie) {
$key_name = date('Ymd', strtotime($briefHistorie['BriefHistory']['created']));
if(!isset($key[$key_name])){
$key[$key_name]=$i;
$i++;
}
$arrayDates[$key[$key_name]][]= $briefHistorie;
}

Related

How to merge multiple dimension array cakephp with two key

I just using function query() of cakePhp. The query will return array something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
)
Now, I want to check if array have the same cate.date and item.code will merge array (in this case is elements 0,1 of my array). Output something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '11',
'price' => '150.000'
),
(int) 1 => array(
'rel_data_category' => '10',
'price' => '250.000'
)
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
),
'item' => array(
'code' => '2'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
),
'item' => array(
'code' => '1'
)
)
)
Please help!
To do that
First declare a variable which later you will store new merge array data
$filter_data = array();
then loop your existing array
foreach($items as $item) {
// do somethind
then create a function which check if item which same date and code not yet exist in the new array
// checker
if (!checkExist($item['cate']['date'], $item['item']['code'])) {
Create a variable for temporary data that will append to the new array later
$data = array(
'cate' => $item['cate'],
'item' => $item['item']
);
Then create loop again which will append all index item cate_detail and cate_item who have the same
date and code from the parent loop
}
}
So your code would be like this
$items = array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
);
$filter_data = array();
foreach($items as $item) {
if (!checkExist($item['cate']['date'], $item['item']['code'])) {
$data = array(
'cate' => $item['cate'],
'item' => $item['item']
);
foreach($items as $detail) {
if ($detail['cate']['date'] == $item['cate']['date'] &&
$detail['item']['code'] == $item['item']['code']) {
$data['cate_detail'][] = array(
'rel_data_category' => $detail['cate_detail']['rel_data_category'],
'price' => $detail['cate_item']['price']
);
}
}
$filter_data[] = $data;
}
}
function checkExist($date, $code){
global $filter_data;
foreach($filter_data as $data) {
if ($data['cate']['date'] == $date &&
$data['item']['code'] == $code) {
return true;
}
}
return false;
}
pr($filter_data);
Demo
OR
$items = array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
);
$filter_data = array();
$exists = array();
foreach($items as $item) {
$index_key = $item['cate']['date'].'-'.$item['item']['code'];
if (!isset($exists[$index_key])) {
$exists[$index_key] = 1;
$data = array(
'cate' => $item['cate'],
'item' => $item['item'],
'cate_detail' => getItems($item['cate']['date'], $item['item']['code'])
);
$filter_data[] = $data;
}
}
function getItems($date, $code) {
global $items;
$data = array();
foreach($items as $detail) {
if ($detail['cate']['date'] == $date && $detail['item']['code'] == $code) {
$data[] = array(
'rel_data_category' => $detail['cate_detail']['rel_data_category'],
'price' => $detail['cate_item']['price']
);
}
}
return $data;
}
pr($filter_data);
Demo

CakePHP get an ordered hash as a result of query

I create a web application which is using CakePHP2. I build up a query with CakePHP ORM but the result is not as what I am expected. So my question is: Is it possible to set up query with CakePHP ORM to get result like as I expected.
Here my query:
$addresses = $this->User->Profile->Address->AddressType->find('all', array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'addresses',
'alias' => 'Address',
'type' => 'LEFT',
'conditions' => array(
'AddressType.id = Address.address_type_id'
)
),
array(
'table' => 'profiles',
'alias' => 'Profile',
'type' => 'LEFT',
'conditions' => array(
'Address.profile_id = Profile.id'
),
),
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'User.profile_id = Profile.id'
),
)
),
'conditions' => array(
'User.id' => $id
),
//'group' => array('AddressType.id'),
'fields' => array(
'Address.*',
'AddressType.*',
'Profile.id',
'User.id'
),
));
Here my result:
array(
(int) 0 => array(
'Address' => array(
'id' => '4',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '5',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '6',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'Test 3',
'zip' => 'Test 3',
'city' => 'Test 3',
'street' => 'Test 3',
'number' => 'Test 3',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 3 => array(
'Address' => array(
'id' => '7',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Test 4',
'zip' => 'Test 4',
'city' => 'Test 4',
'street' => 'Test 4',
'number' => '',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 4 => array(
'Address' => array(
'id' => '8',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test5',
'zip' => 'test5',
'city' => 'test5',
'street' => 'test5',
'number' => 'test5',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 5 => array(
'Address' => array(
'id' => '9',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Tesztecske',
'zip' => 'Test 41',
'city' => 'Test 41',
'street' => 'Test 41',
'number' => 'test',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 6 => array(
'Address' => array(
'id' => '10',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)2222758',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 7 => array(
'Address' => array(
'id' => '13',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'sdfgdfgsdfgdsfg',
'zip' => 'sdfg',
'city' => 'sdfg',
'street' => 'sdfgsdfg',
'number' => 'sdfgsdfg',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
)
)
So i want result which is grouped by address type name.
array(
'Shipping Address' =>array(
(int) 0 => array(
'Address' => array(
'id' => '4',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '8',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test5',
'zip' => 'test5',
'city' => 'test5',
'street' => 'test5',
'number' => 'test5',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '6',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'Test 3',
'zip' => 'Test 3',
'city' => 'Test 3',
'street' => 'Test 3',
'number' => 'Test 3',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
),
'test_type' => array(
(int) 0 => array(
'Address' => array(
'id' => '7',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Test 4',
'zip' => 'Test 4',
'city' => 'Test 4',
'street' => 'Test 4',
'number' => '',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '9',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Tesztecske',
'zip' => 'Test 41',
'city' => 'Test 41',
'street' => 'Test 41',
'number' => 'test',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '5',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
),
'Invoice Address' =>array(
(int) 0 => array(
'Address' => array(
'id' => '10',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)2222758',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '13',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'sdfgdfgsdfgdsfg',
'zip' => 'sdfg',
'city' => 'sdfg',
'street' => 'sdfgsdfg',
'number' => 'sdfgsdfg',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
)
)
)

CakePHP 2.x get data from HABTM table

I have 3 models in my CakePHP application that are linked with a HABTM association following the CakePHP naming conventions: posts - post_tag_links - tags
In my front-end postController.php file, I want to get query all the posts with their related tags. With my query, I can get the information out of the Post en the PostTagLink model, but I don't now how I can reach the info of the Tag model so I can get the tag titles and the tag slugs. Here's my attempt:
$this->Post->virtualFields = array(
'date_published' => 'DATE_FORMAT(Post.published, "%d/%m/%Y")',
'time_published' => 'DATE_FORMAT(Post.published, "%ku%i")',
'day' => 'DATE_FORMAT(Post.published, "%d")',
'month' => 'DATE_FORMAT(Post.published, "%m")',
'year' => 'DATE_FORMAT(Post.published, "%Y")',
'hours' => 'DATE_FORMAT(Post.published, "%k")',
'minutes' => 'DATE_FORMAT(Post.published, "%i")'
);
$this->Paginator->settings = array(
'fields' => array(
'Post.id',
'Post.title',
'Post.content',
'Post.published',
'Post.slug',
'Post.date_published',
'Post.time_published',
'Post.day',
'Post.month',
'Post.year'
),
'conditions' => array(
'Post.published <' => date('Y-m-d H:i:s'),
'Post.show' => 'Y',
'Post.deleted' => null
),
'order' => array(
'Post.published' => 'DESC',
'Post.id' => 'DESC',
),
'limit' => 10
);
$posts = $this->Paginator->paginate(
'Post'
);
The output from this code is close but not close enough:
array(
(int) 0 => array(
'Post' => array(
'id' => '1',
'title' => 'Eerste post',
'content' => 'Content 1',
'published' => '2016-03-16 18:56:00',
'slug' => 'eerste-post',
'date_published' => '16/03/2016',
'time_published' => '18u56',
'day' => '16',
'month' => '03',
'year' => '2016'
),
'PostTagLink' => array(
(int) 0 => array(
'id' => '30',
'post_id' => '1',
'tag_id' => '1',
'created' => '2016-03-05 14:05:48',
'modified' => '2016-03-05 14:05:48',
'deleted' => null
),
(int) 1 => array(
'id' => '31',
'post_id' => '1',
'tag_id' => '3',
'created' => '2016-03-05 15:10:53',
'modified' => '2016-03-05 15:10:53',
'deleted' => null
),
(int) 2 => array(
'id' => '32',
'post_id' => '1',
'tag_id' => '2',
'created' => '2016-03-05 15:10:53',
'modified' => '2016-03-05 15:10:53',
'deleted' => null
),
(int) 3 => array(
'id' => '36',
'post_id' => '1',
'tag_id' => '5',
'created' => '2016-03-20 01:59:41',
'modified' => '2016-03-20 01:59:41',
'deleted' => null
)
)
),
(int) 1 => array(
'Post' => array(
'id' => '2',
'title' => 'Tweede post',
'content' => 'Content 2',
'published' => '2016-02-29 18:59:00',
'slug' => 'tweede-post',
'date_published' => '29/02/2016',
'time_published' => '18u59',
'day' => '29',
'month' => '02',
'year' => '2016'
),
'PostTagLink' => array(
(int) 0 => array(
'id' => '37',
'post_id' => '2',
'tag_id' => '6',
'created' => '2016-03-20 01:59:56',
'modified' => '2016-03-20 01:59:56',
'deleted' => null
)
)
),
(int) 2 => array(
'Post' => array(
'id' => '3',
'title' => 'Derde post',
'content' => 'Content 3',
'published' => '2016-01-22 19:00:00',
'slug' => 'derde-post',
'date_published' => '22/01/2016',
'time_published' => '19u00',
'day' => '22',
'month' => '01',
'year' => '2016'
),
'PostTagLink' => array(
(int) 0 => array(
'id' => '26',
'post_id' => '3',
'tag_id' => '4',
'created' => '2016-01-23 14:12:52',
'modified' => '2016-01-23 14:12:52',
'deleted' => null
),
(int) 1 => array(
'id' => '34',
'post_id' => '3',
'tag_id' => '1',
'created' => '2016-03-09 22:24:33',
'modified' => '2016-03-09 22:24:33',
'deleted' => null
)
)
),
(int) 3 => array(
'Post' => array(
'id' => '4',
'title' => 'Vierde post',
'content' => 'Content 4',
'published' => '2016-01-11 19:00:00',
'slug' => 'vierde-post',
'date_published' => '11/01/2016',
'time_published' => '19u00',
'day' => '11',
'month' => '01',
'year' => '2016'
),
'PostTagLink' => array(
(int) 0 => array(
'id' => '33',
'post_id' => '4',
'tag_id' => '1',
'created' => '2016-03-09 22:24:25',
'modified' => '2016-03-09 22:24:25',
'deleted' => null
)
)
),
(int) 4 => array(
'Post' => array(
'id' => '5',
'title' => 'Vijfde post',
'content' => 'Content 5',
'published' => '2015-05-23 09:54:00',
'slug' => 'vijfde-post',
'date_published' => '23/05/2015',
'time_published' => '9u54',
'day' => '23',
'month' => '05',
'year' => '2015'
),
'PostTagLink' => array(
(int) 0 => array(
'id' => '25',
'post_id' => '5',
'tag_id' => '2',
'created' => '2016-01-23 14:11:22',
'modified' => '2016-01-23 14:11:22',
'deleted' => null
),
(int) 1 => array(
'id' => '27',
'post_id' => '5',
'tag_id' => '4',
'created' => '2016-01-23 14:14:11',
'modified' => '2016-01-23 14:14:11',
'deleted' => null
),
(int) 2 => array(
'id' => '29',
'post_id' => '5',
'tag_id' => '1',
'created' => '2016-02-27 17:02:02',
'modified' => '2016-02-27 17:02:02',
'deleted' => null
),
(int) 3 => array(
'id' => '38',
'post_id' => '5',
'tag_id' => '5',
'created' => '2016-03-20 02:02:14',
'modified' => '2016-03-20 02:02:14',
'deleted' => null
),
(int) 4 => array(
'id' => '39',
'post_id' => '5',
'tag_id' => '7',
'created' => '2016-03-20 02:20:34',
'modified' => '2016-03-20 02:20:34',
'deleted' => null
)
)
)
)
As you can see, there's no additional Tag information available: I only got the link table information. One of my attempts was to add the joins to the query:
$this->Post->virtualFields = array(
'date_published' => 'DATE_FORMAT(Post.published, "%d/%m/%Y")',
'time_published' => 'DATE_FORMAT(Post.published, "%ku%i")',
'day' => 'DATE_FORMAT(Post.published, "%d")',
'month' => 'DATE_FORMAT(Post.published, "%m")',
'year' => 'DATE_FORMAT(Post.published, "%Y")',
'hours' => 'DATE_FORMAT(Post.published, "%k")',
'minutes' => 'DATE_FORMAT(Post.published, "%i")'
);
$this->Paginator->settings = array(
'fields' => array(
'Post.id',
'Post.title',
'Post.content',
'Post.published',
'Post.slug',
'Post.date_published',
'Post.time_published',
'Post.day',
'Post.month',
'Post.year'
),
'joins' => array(
array(
'table' => 'post_tag_links',
'alias' => 'PostTagLink',
'type' => 'inner',
'conditions' => 'Post.id = PostTagLink.post_id'
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'inner',
'conditions' => 'Tag.id = PostTagLink.tag_id'
)
),
'conditions' => array(
'Post.published <' => date('Y-m-d H:i:s'),
'Post.show' => 'Y',
'Post.deleted' => null,
$tagCondition
),
'order' => array(
'Post.published' => 'DESC',
'Post.id' => 'DESC',
),
'limit' => 999
);
$posts = $this->Paginator->paginate(
'Post'
);
Here's the problem that I have multiple times the same Post in the query results withouh returning the Tag information of course :P.
Anyone who can help me on this one? ;)
Edit
Here are my model declarations.
Post.php
App::uses('AppModel', 'Model');
class Post extends AppModel
{
public $hasMany = array(
'PostComment' => array(
'className' => 'PostComment',
'foreignKey' => 'post_id',
'dependent' => true
),
'PostPhoto' => array(
'className' => 'PostPhoto',
'foreignKey' => 'post_id',
'order' => 'PostPhoto.sequence ASC',
'conditions' => array(
'PostPhoto.show' => 'Y'
),
'dependent' => true
),
'PostTagLink' => array(
'className' => 'PostTagLink',
'foreignKey' => 'post_id'
)
);
}
PostTagLink.php
App::uses('AppModel', 'Model');
class PostTagLink extends AppModel
{
public $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id'
),
'Tag' => array(
'className' => 'Tag',
'foreignKey' => 'tag_id'
)
);
}
Tag.php
App::uses('AppModel', 'Model');
class Tag extends AppModel
{
public $hasMany = array(
'PostTagLink' => array(
'className' => 'PostTagLink',
'foreignKey' => 'tag_id'
)
);
}

CakePHP 2.4.0 Recusion Inside of Contain in Paginator->settings Not Working

I'm having an issue with Paginator->settings. For some reason, recursive is not working. Any ideas? I've tried setting $this->Cheese->recursive = 2 and haven't had any luck.
$this->Cheese->recursive = 2;
$this->Paginator->settings = array(
'CheeseCheckin' => array(
'fields' => array('id','created','comment','location','rating','short_url_hash','foursquare_id'),
'conditions'=>array(
'CheeseCheckin.cheese_id' => $id
),
'contain' => array(
'Cheese'=>array(
'fields' => array('id','name','created','modified','attachment_id'),
'Attachment' => array(
'fields' => array('id','name','ext','path')
)
),
'CheeseProducer' => array(
'fields' => array('name','created', 'id','attachment_id')
),
'User' => array(
'fields' => $this->userFields,
'Attachment' => array(
'fields'=>array('id','name','ext','path')
),
'StateRegion' => array(
'fields'=>array('name','code')
),
'Country' => array(
'fields'=>array('name','code')
)
),
'Place' => array(
'fields'=>array('id','name')
),
'UserAttachment' => array(
'fields' => array('id','ext','name','path')
)
),
'page' => $page,
'order' => array('CheeseCheckin.created' => 'DESC'),
'limit' => $limit,
'recursive' => 2
)
);
debug($this->Paginator->paginate('CheeseCheckin'));
The results look like:
array(
(int) 0 => array(
'CheeseCheckin' => array(
'id' => '62',
'created' => '2013-09-06 13:34:44',
'comment' => 'This is a test',
'location' => null,
'rating' => '4',
'short_url_hash' => '18ARMkq',
'foursquare_id' => '40d77680f964a5205d011fe3'
),
'UserAttachment' => array(
'id' => null,
'ext' => null,
'name' => null,
'path' => null
),
'User' => array(
'id' => '1',
'username' => '...',
'name' => 'Rob',
'profile_image_url' => '...',
'active' => '1',
'email' => 'robksawyer#gmail.com',
'private' => false,
'attachment_id' => '616',
'country_id' => '228',
'state_region_id' => '48'
),
'Cheese' => array(
'id' => '379',
'name' => 'Party In a Jar',
'created' => '2012-10-05 16:57:04',
'modified' => '2013-09-13 02:12:27',
'attachment_id' => '460'
),
'CheeseProducer' => array(
'name' => 'Alsea Acre Goat Cheese',
'created' => '2012-10-05 16:50:51',
'id' => '43',
'attachment_id' => null
),
'Place' => array(
'id' => null,
'name' => null
)
)
)
Fixed the issue. I just had to add user_id, attachment_id, etc. to the fields array.

rearrange multidimensional array in cakephp 2.2

I have this multidimensional assoc $modules array in cakephp 2.2.3:
array(
'type1' => array(
(int) 0 => array(
'name' => 'name16',
'type' => '1',
'category' => 'categoryC',
'zone' => 'zone1'
),
(int) 1 => array(
'name' => 'name17',
'type' => '1',
'category' => 'categoryB',
'zone' => 'zone1'
),
(int) 2 => array(
'name' => 'name18',
'type' => '1',
'category' => 'categoryA',
'zone' => 'zone3'
),
(int) 3 => array(
'name' => 'name19',
'type' => '1',
'category' => 'categoryC',
'zone' => 'zone3'
),
(int) 4 => array(
'name' => 'name22',
'type' => '1',
'category' => 'categoryA',
'zone' => 'zone2'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name1',
'type' => '2',
'category' => 'categoryB',
'zone' => 'zone2'
),
(int) 1 => array(
'name' => 'name2',
'type' => '2',
'category' => 'categoryB',
'zone' => 'zone2'
),
(int) 2 => array(
'name' => 'name3',
'type' => '2',
'category' => 'categoryA',
'zone' => 'zone1'
),
(int) 3 => array(
'name' => 'name4',
'type' => '2',
'category' => 'categoryC',
'zone' => 'zone3'
),
(int) 4 => array(
'name' => 'name5',
'type' => '2',
'category' => 'categoryA',
'zone' => 'zone3'
)
)
)
and I want to rearrange it like
array(
'zone1' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name17',
'type' => '1',
'category' => 'categoryB'
),
(int) 1 => array(
'name' => 'name16',
'type' => '1',
'category' => 'categoryC'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name3',
'type' => '2',
'category' => 'categoryA'
)
)
),
'zone2' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name22',
'type' => '1',
'category' => 'categoryA'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name1',
'type' => '2',
'category' => 'categoryB'
),
(int) 1 => array(
'name' => 'name2',
'type' => '2',
'category' => 'categoryB'
)
)
),
'zone3' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name18',
'type' => '1',
'category' => 'categoryA'
),
(int) 0 => array(
'name' => 'name19',
'type' => '1',
'category' => 'categoryC'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name5',
'type' => '2',
'category' => 'categoryA'
),
(int) 1 => array(
'name' => 'name4',
'type' => '2',
'category' => 'categoryC'
)
)
)
)
where 'category' should be sorted alphabetically within each parent 'type'
I tried with
$modules = Hash::combine($modules, '{n}', '{n}.{n}', '{s}.{s}.{n}.zone');
but with no success. I guess I'm not using Hash::combine as I should...
Thanks, Marius
Just use a foreach inside a foreach to walk through your array and construct a new one based on your preference.

Categories