In one of my forms I have a text input for a posts tags. At the moment, Cake is returning the tag id into this field rather than the name of the tag. I want to change it to show the name of the tag.
The posts controller is getting a result like this:
array(
'Post' => array(
'id' => '7',
'title' => 'Testing again',
'body' => 'wooooooo',
'created' => '2013-01-09 19:20:53',
'slug' => 'testing-again'
),
'Tag' => array(
(int) 0 => array(
'id' => '4',
'name' => 'tag1'
),
(int) 1 => array(
'id' => '3',
'name' => 'tag2'
),
(int) 2 => array(
'id' => '5',
'name' => 'tag3'
)
)
)
and the form is laid out like this:
<?php echo $this->Form->create('Post'); ?>
<?php echo $this->Form->input('Post.title'); ?>
<?php echo $this->Form->input('Post.body'); ?>
<?php echo $this->Form->input('Tag.Tag', array('type' => 'text', 'label' => 'Tags (seperated by space)')); ?>
<?php echo $this->Form->input('Post.slug'); ?>
<?php echo $this->Form->end('Save Changes'); ?>
Is there a way I can tell CakePHP to output the name field of the tags instead of id? Thanks.
OK, so from what I’ve gathered from our comment discussion, this would be what you’re looking for. In your controller, just loop over all the set tags for the post. Assuming your find result is set in the
$postvariable, you can use the below code and save them all in a “plain” non-recursive array:Then in your view you can just
implodethe array with a space and set it as value for your text field:The find example in your OP would then return a value of
tag1 tag2 tag3.