Model_Comment を持つ可能性のある Model_Post があるとします。
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();