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