Re: how can I make a slug using fields from a different table using sluggable behaviour
On Tue, Apr 19, 2011 at 8:08 AM, barricades <davowave@googlemail.com> wrote:
> Thanks for all the help. I've been fiddling around with what you gave
> me and I understand how it works. I've also had another look at the
> sluggable behaviour and although I still don't completely understand
> it I can see that it's also doing a lot of things which the beforeSave
> function you gave me doesn't.
>
> If I wanted to continue to use the sluggable behaviour but still have
> the slug made from a different table could I just alter the beforeSave
> in sluggable somehow?
>
> Or even where you specify the fields in the actsAs array, can you not
> specify fields are from certain models?
No, because the behavior gets the strings to work with from the
model's copy of the data that's been set.
The simplest way to handle this would be to create a new column,
username, in the campaigns table. Set that as the field to be
sluggified in the $actsAs array. (Make sure you have a "slug" column,
as well.) In your controller:
$this->data['Campaign']['username'] = $data['User']['first_name']. ' '
. $data['User']['last_name'];
(That's a space between them.)
This assumes that the User data is inside $this->data at the time, of
course. If you want to capture the name of the logged-in User who is
actually saving the Campaign:
$this->data['Campaign']['username'] = $this->Auth->user('first_name')
. ' ' . $this->Auth->user('last_name');
If an admin will be creating Campaigns *on behalf* of some User, then
the first form will be necessary and presumably the admin will be
choosing a User from a list.
So now you've got the full User name as well as a slug in the
campaigns table. So you can use ['Campaign']['slug'] for your URLs,
and ['Campaign']['username'] for display on the page.
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home