Thursday, September 30, 2010

Re: beforeCreate() and afterCreate(): better implementation than mine?

Thanks! This makes the code much cleaner.

On Thu, Sep 30, 2010 at 4:39 PM, euromark <dereuromark@googlemail.com> wrote:
> afterSave already contains "created/updated":
> http://book.cakephp.org/view/684/afterSave
> note the param passed
>
> On 30 Sep., 16:35, psybear83 <psybea...@gmail.com> wrote:
>> Hello folks
>>
>> From Ruby On Rails I know the beforeCreate() and afterCreate() hooks
>> of models. I didn't find them for CakePHP, so I implemented them
>> myself:
>>
>>   function isNewRecord() {
>>     return empty($this->id) || $this->field('created') == $this->field('modified');
>>
>>   }
>>
>>   function beforeSave() {
>>     if($this->isNewRecord()) {
>>       return $this->beforeCreate();
>>     }
>>   }
>>
>>   function afterSave() {
>>     if($this->isNewRecord()) {
>>       return $this->afterCreate();
>>     }
>>   }
>>
>>   function beforeCreate() {
>>     return true;
>>   }
>>
>>   function afterCreate() {
>>     return true;
>>   }
>>
>> Sadly, this implementation depends on created and modified fields, and
>> the isNewRecord() method itself isn't very beautiful, too. So maybe
>> there is a better implementation?
>>
>> Thanks
>> Josh
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> 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?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: