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, [email protected] [via CakePHP] wrote:
>
>
> On May 14, 5:05 pm, Thomas Ploch <[hidden email]
> </user/SendEmail.jtp?type=node&node=5707935&i=0>> 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] </user/SendEmail.jtp?type=node&node=5707935&i=1> 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
> [email protected]
> To unsubscribe from CakePHP, click here
> <http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1255722&code=YmlsbC5zdG9sdHpAYm9vc3RlcndlYnNvbHV0aW9ucy5jb218MTI1NTcyMnwtNTU0NTk2MTUy>.
> NAML
> <http://cakephp.1045679.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
>
--
View this message in context:
http://cakephp.1045679.n5.nabble.com/How-to-tell-if-it-s-an-edit-or-create-action-tp5707928p5707937.html
Sent from the CakePHP mailing list archive at Nabble.com.
--
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