CakePHP get an ordered hash as a result of query - php

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'
)
)
)
)

Related

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'
)
);
}

PHP string array casting

I'm trying to find a way to cast the following into a real array. The data is stored as a string in the system I am working on.
I have tried (array) $stringArrayData; but that didn't work.
The string data is as follows:
array(
4 => array(
'nid' => 4099,
'cid' => '4',
'pid' => '0',
'form_key' => 'event_details',
'name' => 'Event Details',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '1',
'page_num' => 1,
),
5 => array(
'nid' => 4099,
'cid' => '5',
'pid' => '4',
'form_key' => 'name',
'name' => 'Name',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '2',
'page_num' => 1,
),
6 => array(
'nid' => 4099,
'cid' => '6',
'pid' => '4',
'form_key' => 'description',
'name' => 'Description',
'type' => 'textarea',
'value' => '',
'extra' => array(
'description' => 'please include who the event is aimed at',
'title_display' => 0,
'private' => 0,
'resizable' => 0,
'disabled' => 0,
'conditional_operator' => '=',
'cols' => '',
'rows' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '3',
'page_num' => 1,
),
21 => array(
'nid' => 4099,
'cid' => '21',
'pid' => '4',
'form_key' => 'add_an_image',
'name' => 'Add an image',
'type' => 'file',
'value' => '',
'extra' => array(
'scheme' => 'public',
'directory' => 'event-suggestions',
'title_display' => 'before',
'private' => 0,
'progress_indicator' => 'throbber',
'filtering' => array(
'size' => '2 MB',
'types' => array(
'gif',
'jpg',
'png',
),
'addextensions' => '',
),
'conditional_operator' => '=',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '4',
'page_num' => 1,
),
7 => array(
'nid' => 4099,
'cid' => '7',
'pid' => '4',
'form_key' => 'venue',
'name' => 'Venue',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '5',
'page_num' => 1,
),
23 => array(
'nid' => 4099,
'cid' => '23',
'pid' => '7',
'form_key' => 'please_select_your_venue',
'name' => 'Please select your venue',
'type' => 'select',
'value' => '',
'extra' => array(
'items' => "3257|639 Enterprise Centre\n3267|Alexandra Palace\n126|Alexandra Park Library\n142|Asian Centre\n153|Bernie Grant Arts Centre\n127|Bruce Castle Museum\n3259|Community Use For the Old Station\n128|Coombes Croft Library\n147|Crowland Primary School\n146|Delicia Cafe\n138|Fortismere School\n3385|Haringey Sixth Form Centre\n135|Harmony Gardens Wins!\n149|Highgate Gallery\n129|Highgate Library\n130|Hornsey Library\n154|Jacksons Lane\n148|Kurdish Community Centre\n155|Lauderdale House\n3235|Living Under One Sun Community Allotment\n132|Marcus Garvey Library\n139|Methodist Church Hall\n133|Muswell Hill Library\n150|Northumberland Park Neighbourhood Resource Centre\n141|Open Smart Homes around Haringey\n156|Park Road Leisure Centre\n134|St Ann's Library\n157|Stroud Green and Harringay Library\n145|T Chances\n144|The Beehive Pub\n140|The Old Dairy\n136|The Tagore Centre\n143|Tottenham Community Sports Centre\n158|Tottenham Green Leisure Centre\n152|Tottenham Seventh-Day Adventist Church\n137|Triangle Centre\n151|Victoria Stakes\n159|White Hart Lane Community Sports Centre\n131|Wood Green Central Library\nother|Other\n",
'options_source' => 'event_venue',
'multiple' => 0,
'title_display' => 'none',
'private' => 0,
'aslist' => 1,
'optrand' => 0,
'other_option' => NULL,
'other_text' => 'Other...',
'description' => '',
'custom_keys' => FALSE,
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '12',
'page_num' => 1,
),
27 => array(
'nid' => 4099,
'cid' => '27',
'pid' => '7',
'form_key' => 'other_venue',
'name' => 'Other Venue',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 'none',
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '14',
'page_num' => 1,
),
28 => array(
'nid' => 4099,
'cid' => '28',
'pid' => '27',
'form_key' => 'venue_company_name',
'name' => 'Venue/Company name',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '15',
'page_num' => 1,
),
29 => array(
'nid' => 4099,
'cid' => '29',
'pid' => '27',
'form_key' => 'address_line_1',
'name' => 'Address Line 1',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'before',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '16',
'page_num' => 1,
),
30 => array(
'nid' => 4099,
'cid' => '30',
'pid' => '27',
'form_key' => 'address_line_2',
'name' => 'Address Line 2',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'before',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '17',
'page_num' => 1,
),
31 => array(
'nid' => 4099,
'cid' => '31',
'pid' => '27',
'form_key' => 'town_city',
'name' => 'Town/City',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'before',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '18',
'page_num' => 1,
),
32 => array(
'nid' => 4099,
'cid' => '32',
'pid' => '27',
'form_key' => 'county',
'name' => 'County',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'before',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '19',
'page_num' => 1,
),
33 => array(
'nid' => 4099,
'cid' => '33',
'pid' => '27',
'form_key' => 'postcode',
'name' => 'Postcode',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'before',
'private' => 0,
'width' => '20',
'disabled' => 0,
'unique' => 0,
'maxlength' => '7',
'webform_conditional_field_value' => 'other',
'webform_conditional_cid' => '23',
'webform_conditional_operator' => '=',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '20',
'page_num' => 1,
),
8 => array(
'nid' => 4099,
'cid' => '8',
'pid' => '4',
'form_key' => 'dates',
'name' => 'Dates',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '6',
'page_num' => 1,
),
9 => array(
'nid' => 4099,
'cid' => '9',
'pid' => '8',
'form_key' => 'starting',
'name' => 'Starting',
'type' => 'date',
'value' => '',
'extra' => array(
'timezone' => 'user',
'title_display' => 'before',
'private' => 0,
'datepicker' => 1,
'year_textfield' => 0,
'start_date' => '0 years',
'end_date' => '+2 years',
'description' => '',
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '7',
'page_num' => 1,
),
25 => array(
'nid' => 4099,
'cid' => '25',
'pid' => '8',
'form_key' => 'number_of_occurrences',
'name' => 'Number of occurrences',
'type' => 'select',
'value' => '1',
'extra' => array(
'items' => "1|1\n2|2\n3|3\n4|4\n5|5\n6|6\n7|7\n8|8\n9|9\n10|10",
'multiple' => 0,
'title_display' => 'before',
'private' => 0,
'aslist' => 1,
'optrand' => 0,
'other_option' => NULL,
'other_text' => 'Other...',
'description' => '',
'custom_keys' => FALSE,
'options_source' => '',
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '8',
'page_num' => 1,
),
11 => array(
'nid' => 4099,
'cid' => '11',
'pid' => '8',
'form_key' => 'how_often_does_this_event_occur',
'name' => 'How often does this event occur?',
'type' => 'select',
'value' => '',
'extra' => array(
'items' => "655|One off\n656|Daily\n657|Weekly\n658|Forthnightly\n659|Monthly\n660|Other\n",
'options_source' => 'event_frequency',
'multiple' => 0,
'title_display' => 'before',
'private' => 0,
'aslist' => 1,
'optrand' => 0,
'other_option' => NULL,
'other_text' => 'Other...',
'description' => '',
'custom_keys' => FALSE,
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '9',
'page_num' => 1,
),
13 => array(
'nid' => 4099,
'cid' => '13',
'pid' => '4',
'form_key' => 'event_contact_details',
'name' => 'Event Contact Details ',
'type' => 'textarea',
'value' => '',
'extra' => array(
'description' => 'Please note that these contact details WILL be published with the event details, in case customers want any more information',
'title_display' => 0,
'private' => 0,
'resizable' => 0,
'disabled' => 0,
'conditional_operator' => '=',
'cols' => '',
'rows' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '7',
'page_num' => 1,
),
1 => array(
'nid' => 4099,
'cid' => '1',
'pid' => '0',
'form_key' => 'your_contact_details',
'name' => 'Your Contact Details',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'description' => 'Please note that your contact details are for internal use only in case we have a query with the details submitted. These details will not be published on the site.',
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '2',
'page_num' => 1,
),
2 => array(
'nid' => 4099,
'cid' => '2',
'pid' => '1',
'form_key' => 'full_name',
'name' => 'Full Name',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'conditional_operator' => '=',
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '1',
'page_num' => 1,
),
24 => array(
'nid' => 4099,
'cid' => '24',
'pid' => '1',
'form_key' => 'phone_number',
'name' => 'Phone Number',
'type' => 'textfield',
'value' => '',
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'width' => '',
'maxlength' => '',
'field_prefix' => '',
'field_suffix' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '2',
'page_num' => 1,
),
3 => array(
'nid' => 4099,
'cid' => '3',
'pid' => '1',
'form_key' => 'email_address',
'name' => 'Email Address',
'type' => 'email',
'value' => '',
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'disabled' => 0,
'unique' => 0,
'conditional_operator' => '=',
'width' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '3',
'page_num' => 1,
),
14 => array(
'nid' => 4099,
'cid' => '14',
'pid' => '0',
'form_key' => 'admission_details',
'name' => 'Admission Details',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '3',
'page_num' => 1,
),
15 => array(
'nid' => 4099,
'cid' => '15',
'pid' => '14',
'form_key' => 'cost',
'name' => 'Cost',
'type' => 'select',
'value' => '',
'extra' => array(
'items' => "616|Free Event\n628|Under £10\n630|£10 or over\n",
'options_source' => 'event_cost',
'multiple' => 0,
'title_display' => 'before',
'private' => 0,
'aslist' => 1,
'optrand' => 0,
'other_option' => NULL,
'other_text' => 'Other...',
'description' => '',
'custom_keys' => FALSE,
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '3',
'page_num' => 1,
),
16 => array(
'nid' => 4099,
'cid' => '16',
'pid' => '14',
'form_key' => 'concessions_etc',
'name' => 'Concessions etc',
'type' => 'textarea',
'value' => '',
'extra' => array(
'description' => 'Please add any additional pricing details, including any available concessions',
'title_display' => 0,
'private' => 0,
'resizable' => 0,
'disabled' => 0,
'conditional_operator' => '=',
'cols' => '',
'rows' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '4',
'page_num' => 1,
),
17 => array(
'nid' => 4099,
'cid' => '17',
'pid' => '0',
'form_key' => 'type_of_event',
'name' => 'Type of event',
'type' => 'fieldset',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'collapsible' => 0,
'collapsed' => 0,
'conditional_operator' => '=',
'description' => '',
'conditional_component' => '',
'conditional_values' => '',
),
'mandatory' => '0',
'weight' => '4',
'page_num' => 1,
),
18 => array(
'nid' => 4099,
'cid' => '18',
'pid' => '17',
'form_key' => 'event_category',
'name' => 'Event Category',
'type' => 'select',
'value' => '',
'extra' => array(
'items' => "615|Advice, jobs and training\n629|Art and crafts\n646|Children and families\n653|Cinema/film\n654|Comedy\n619|Community events\n648|Energy efficiency scheme\n649|Food market\n618|Health and well-being\n647|Literature\n636|Local history\n623|Music\n632|Nature and wildlife\n652|Other\n622|Play session\n639|Sports\n642|Theatre and dance\n",
'options_source' => 'event_category',
'multiple' => 1,
'title_display' => 'none',
'private' => 0,
'aslist' => 0,
'optrand' => 0,
'other_option' => NULL,
'other_text' => 'Other...',
'description' => '',
'custom_keys' => FALSE,
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '4',
'page_num' => 1,
),
19 => array(
'nid' => 4099,
'cid' => '19',
'pid' => '17',
'form_key' => 'please_give_details',
'name' => 'Please give any other details',
'type' => 'textarea',
'value' => '',
'extra' => array(
'title_display' => 0,
'private' => 0,
'resizable' => 0,
'disabled' => 0,
'webform_conditional_field_value' => "Other\n652",
'webform_conditional_cid' => '18',
'webform_conditional_operator' => '=',
'cols' => '',
'rows' => '',
'description' => '',
'attributes' => array(),
'conditional_component' => '',
'conditional_operator' => '=',
'conditional_values' => '',
),
'mandatory' => '1',
'weight' => '5',
'page_num' => 1,
),
)
If you have the option, preferably, you should store the items individually - It will be much easier to change parts of the data, and will allow you to abstract the language you're using away from the data storage
If that's not an option, consider storing the data in a different way - Maybe serialize the data before you save it to the DB, and then unserialize it when you retrieve it. Unserialize keeps the data type and structure.
If you can't do that, then you would probably have to parse the string and build a new array from that, or eval the entire string to a variable and then use the variable.
Try this:
eval("\$array = " . $stringArrayData);
Then,
print_r($array);
# try this:
# fileA:array.txt(file contains the strings)
# fileB:
$ar_source = include 'array.txt';
var_dump($ar_source );

how to categories(group - classify) array by date

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;
}

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