Barricades,
I think your concern about the beforeSave/afterSave issues are red
herrings. Since the first line of your code is:
if ($this->Campaign->save($this->data)) { ...
both the before and after are finished. And you are only getting
inside the 'if' statement when the save was successful. Of course, in
that case the slug exists but it isn't available in the way you are
trying to get to it. That's why I suggested searching on the returned
id of the created record. That will get you the slug value to pass on
through to your redirect. To prove it to yourself, modify the if
statement to read:
if ($this->Campaign->save($this->data)) {
debug($this->Campaign->findById($this->Campaign-
>id,array('fields'=>'slug')));
die;
...
}
So using the find statement I've shown you in the debug function
(above) you could do this:
if ($this->Campaign->save($this->data)) {
$new_record = $this->Campaign->findById(
$this->Campaign->id,
array('fields'=>'slug')
);
$this->redirect(array(
'action' => 'view',
$new_record['Campaign']['slug']
));
} else {
....
}
Regards,
Don
On May 8, 4:59 am, barricades <[email protected]> wrote:
> Thanks for the reply :)
>
> The find after the save isn't returning what I want it to. In fact I'm
> not sure it's returning anything at all. Baring in mind I'm a total
> newbie (not even that much of a php'er either yet) what I want it
> simply somehow (what I've done is just my best guess - I've no idea
> how to do this at all) to make it so that when I save a new Campaign,
> if it's successful, it redirects to a view action and passes along the
> slug, as a string I guess would be best(?), so that I can show the
> details of the newly created Campaign on in the view.ctp.
>
> The caveat being that I'm using sluggable behaviour which creates the
> slug in a beforeSave and therefore the $slug to be passed on to the
> view function has to be created after the save.
>
> I hope that makes sense.
>
> On May 8, 3:08 am, dreamingmind <[email protected]> wrote:
>
>
>
>
>
>
>
> > Barricades,
>
> > You don't really say WHERE the process seems to break down. The find
> > after the save seems suspicious to me. Is that returning the proper
> > slug to pass along to your view function? It looks like $slug will end
> > up holding the returned data array rather than the returned string
> > from the slug field (see below for more info)
>
> > You might take advantage of the fact that the id property of the model
> > is set to the id of the record that was created by save. So you should
> > be able to set your slug value properly after:
>
> > $this->Campaign->findById($this->Campaign-
>
> > >id,,array('fields'=>'slug'));
>
> > But you should also be aware that this will not return the slug field
> > directly. It's going to be in the usual data array:
>
> > Array
> > (
> > [Campaign] => Array
> > (
> > [slug] => My_crazy_slug
> > )
> > )
>
> > Regards,
> > Don
>
> > On May 7, 9:22 am, barricades <[email protected]> wrote:
>
> > > Hi, I'm new so please don't shout at me for the stupid question but...
>
> > > When I save a record I want to redirect straight to the record I just
> > > created. The record which I have just created uses sluggable behaviour
> > > to create a slug in a beforeSave, so I can't just use
> > > $this->data['Campaign]['slug'] as it doesn't exist before I save my
>
> > > campaign.
>
> > > in my add function I've got:
>
> > > <pre>
> > > if ($this->Campaign->save($this->data)) {
> > > $slug = $this->Campaign->find('first', array('fields' =>
> > > 'Campaign.slug'));
> > > $this->redirect(array('action' => 'view', $slug));
> > > } else {
> > > $this->Session->setFlash(__('The campaign could not be saved.
> > > Please,
> > > try again.', true));}
>
> > > </pre>
>
> > > and then
>
> > > <pre>
> > > function view($slug) {
> > > $campaign = $this->Campaign->find('first',
> > > array('conditions'=>array('Campaign.slug'=>$slug), 'recursive'=>-1));
> > > $this->set(compact('campaign'));
> > > }
> > > </pre>
>
> > > but that ain't working. I've tried a couple of other ways but I'm a
> > > bit stumped.
>
> > > How do I get the value that's in the slug column for the Campaign
> > > which has just been saved and pass it on to the view function so that
> > > I can display the right Campaign?
>
> > > thanks in advance :)
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php