假设我有一个 Model_Post,它有_may Model_Comment。
每次保存新评论时,有什么方法updated_at
可以更新 Model_Post 的字段?我检查了观察者的文档,但我找不到如何以“ORM 方式”进行操作。
此代码似乎不起作用:
class Model_Comment extends \Orm\Model
{
protected static $_properties = array(
'id',
'post_id',
'text',
);
protected static $_belongs_to = array('post');
protected static $_observers = array(
'Orm\\Observer_UpdatedAt' => array(
'events' => array('before_save'),
'relations' => array('post')
)
);
}
class Model_Post extends Model
{
protected static $_properties = array(
'id',
'title',
'text',
'updated_at'
);
protected static $_has_many = array('comments');
}
我使用以下代码创建了一个新评论:
$comment = Model_Comment::forge();
$comment->message_id = Input::post('message_id');
$comment->text = Input::post('text');
$comment->save();