abb*_*ood 11 php regex laravel laravel-4
按照这个答案,我在Laravel中建模保存回调(类似于rails),如下所示:
class LessonPlan extends Eloquent {
public function save(array $options = array())
{
// before save code
parent::save();
// after save code
}
}
Run Code Online (Sandbox Code Playgroud)
但是,Page当我保存新页面或更新现有页面时,我会调用save().我怎么知道这个操作中哪个是哪个?
我试过类似的东西
public function save(array $options = array())
{
// before save code
$oldLesson = clone $this;
parent::save();
..
if ($this->isLessonStatusChanged($oldLesson)) {
..
}
}
private function isLessonStatusChanged($oldLesson) {
return $this->status != $oldLesson->status;
}
Run Code Online (Sandbox Code Playgroud)
但这并不好......因为$ oldLesson已经有了$ lesson的新值
我最后做的只是重新编写网址,看看它是否是一个更新请求..但我已经在晚上睡觉时遇到了问题(我的回答并没有真正告诉我是否有任何值实际发生了变化.. b/c one可以提交更新表格,而无需实际更改任何内容)..有更清洁的方法吗?
afa*_*zit 14
您可以使用isDirty()返回a 的方法,bool并getDirty()返回array带有更改值的方法.
public function save(array $options = array())
{
$changed = $this->isDirty() ? $this->getDirty() : false;
// before save code
parent::save();
// Do stuff here
if($changed)
{
foreach($changed as $attr)
{
// My logic
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10279 次 |
| 最近记录: |