Quantcast

How to tell if it's an edit or create action?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to tell if it's an edit or create action?

rahajiyev@bankofbaku.com
In model's beforeSave() I need to do different things based on whether
we're adding or editing an existing object. Unfortunately $this-
>data[$this->alias]['id'] is empty even when editing (but it's in the
URL). How can I _cleanly_ check if we're creating or editing? My first
impression is that CakePHP is a bit flaky :)

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

Jeremy Burns | Class Outfit
Ask yourself this: "If the id column is empty, how does the model work out which row to update?", and then check your code that sends data to the model (the id column to be present and populated on an edit).

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 14 May 2012, at 12:28:43, [hidden email] wrote:

In model's beforeSave() I need to do different things based on whether
we're adding or editing an existing object. Unfortunately $this-
data[$this->alias]['id'] is empty even when editing (but it's in the
URL). How can I _cleanly_ check if we're creating or editing? My first
impression is that CakePHP is a bit flaky :)

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

Thomas Ploch
I suppose that:

a) The hidden input for Model.id is missing from the edit form.
b) The record isn't being read in the action beforehand

Regards
Thomas

Am 14.05.2012 13:38, schrieb Jeremy Burns | Class Outfit:
Ask yourself this: "If the id column is empty, how does the model work out which row to update?", and then check your code that sends data to the model (the id column to be present and populated on an edit).

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 14 May 2012, at 12:28:43, [hidden email] wrote:

In model's beforeSave() I need to do different things based on whether
we're adding or editing an existing object. Unfortunately $this-
data[$this->alias]['id'] is empty even when editing (but it's in the
URL). How can I _cleanly_ check if we're creating or editing? My first
impression is that CakePHP is a bit flaky :)

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

stork
In reply to this post by rahajiyev@bankofbaku.com
Read api/code of

Model::$id
Model::getID()
Model::exists()

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

rahajiyev@bankofbaku.com
In reply to this post by Thomas Ploch


On May 14, 5:05 pm, Thomas Ploch <[hidden email]> wrote:
> I suppose that:
>
> a) The hidden input for Model.id is missing from the edit form.
> b) The record isn't being read in the action beforehand
>

It isn't missing. the edit action receives it as a parameter. It's in
the URL! admin/edit/345
The update itself works fine. I just need to do something else in
beforeSave().

public function admin_edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->isPost() || $this->request->isPut()) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__('The user has been
saved, password: %s', $this->User->cleartext_password)));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be
saved. Please, try again.'));
                $this->set('errors', $this->User->validationErrors);
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

but then I can't get at the id field to check if we're creating or
saving.

        public function beforeSave() {
                $data = &$this->data[$this->alias];
                var_dump($data); exit;
                if (empty($data['id'])) {
                   // do when adding
                } else {
                   // do when editing
                }

                return true;
        }


How?

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

rahajiyev@bankofbaku.com
In reply to this post by stork


On May 14, 5:43 pm, stork <[hidden email]> wrote:
> Read api/code of
>
> Model::$id
> Model::getID()
> Model::exists()

Excellent! $this->getID() did the trick in beforeSave(). Thanks.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to tell if it's an edit or create action?

bs28723
In reply to this post by rahajiyev@bankofbaku.com
I am not sure if adding exists() to beforeSave is another DB query or not.

But, why not add a field in your add and edit functions?

function admin_add($id = null) {
  // other checks
   if ($this->request->isPost() || $this->request->isPut()) {
        $this->request->data['new'] = true;
        if ($this->User->save($this->request->data)) {
            // save
        }
   }
}

function admin_edit($id = null) {
  // other checks
   if ($this->request->isPost() || $this->request->isPut()) {
        $this->request->data['new'] = false;
        if ($this->User->save($this->request->data)) {
            // save
        }
   }
}

function beforeSave() {
    // other checks
          if (!empty($data['new']) && $data['new']===true ) {
                   // do when adding
          } else {
                   // do when editing
          }
        // not sure the unset is needed, since the field does not exist in DB.
        unset($this->request->data['new']);
}


On 5/14/2012 8:50 AM, [hidden email] [via CakePHP] wrote:


On May 14, 5:05 pm, Thomas Ploch <[hidden email]> wrote:
> I suppose that:
>
> a) The hidden input for Model.id is missing from the edit form.
> b) The record isn't being read in the action beforehand
>

It isn't missing. the edit action receives it as a parameter. It's in
the URL! admin/edit/345
The update itself works fine. I just need to do something else in
beforeSave().

public function admin_edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->isPost() || $this->request->isPut()) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__('The user has been
saved, password: %s', $this->User->cleartext_password)));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be
saved. Please, try again.'));
                $this->set('errors', $this->User->validationErrors);
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

but then I can't get at the id field to check if we're creating or
saving.

        public function beforeSave() {
                $data = &$this->data[$this->alias];
                var_dump($data); exit;
                if (empty($data['id'])) {
                   // do when adding
                } else {
                   // do when editing
                }

                return true;
        }


How?

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php



If you reply to this email, your message will be added to the discussion below:
http://cakephp.1045679.n5.nabble.com/How-to-tell-if-it-s-an-edit-or-create-action-tp5707928p5707935.html
To start a new topic under CakePHP, email [hidden email]
To unsubscribe from CakePHP, click here.
NAML
Loading...