0

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.

James Dawson
  • 5,309
  • 20
  • 72
  • 126
  • Aren't the entered tags the `tag1`, `tag2` and `tag3` listed under `Tag.x.name`? – Oldskool Jan 09 '13 at 18:54
  • Sorry, I'm not sure what you mean? – James Dawson Jan 09 '13 at 19:02
  • Well, in the data that is returned to you controller I see an array of tags (numerically indexed) listing names `tag1`, `tag2` and `tag3`. Aren't those the tags you entered in the form? – Oldskool Jan 09 '13 at 19:10
  • I should've been more specific in my OP sorry. This is on a post edit page. Say I submit a blog post with the tags `php`, `mysql` and `oracle` and later I want to edit that posts tags, the edit page displays the ID of those tags rather than the name. I'd like it to display the name so I know what the tags are, if that makes sense. :) – James Dawson Jan 09 '13 at 19:15
  • Basically, the output you see above is from a `find` operation coming from the database. – James Dawson Jan 09 '13 at 19:17

2 Answers2

1

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 $post variable, you can use the below code and save them all in a "plain" non-recursive array:

$tags = array(); // This will hold all the tags
foreach($post['Tag'] as $tag) {
    $tags[] = $tag['name'];
}

// Set the tags as view variable
$this->set(compact('tags'));

Then in your view you can just implode the array with a space and set it as value for your text field:

echo $this->Form->input('Tag.Tag', array('type' => 'text', 'label' => 'Tags (seperated by space)', 'value' => implode(' ', $tags)));

The find example in your OP would then return a value of tag1 tag2 tag3.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • I thought there would be a way to tell Cake to do this, ah well. This works anyway, thank you! :) – James Dawson Jan 09 '13 at 19:42
  • this won't actually edit them when you save it as the `id` is lost. you would need some additional processing – Ross Jan 09 '13 at 19:43
1

Did you set the $displayField in the tags model?

eg.

public $displayField = "name";
Ajay K
  • 354
  • 1
  • 4
  • 14