php - Yii: How to define parent object to related childs? -
php - Yii: How to define parent object to related childs? -
2 tables: threads , comments (e.g.) => thread & comment models
thread.php
public function relations() { homecoming array( 'comments'=>array(self::has_many, 'comment', 'thread_id'), ); }
how define property parent thread object each comment child?
something this:
$model = thread::model()->with('comments')->findall(); foreach($model->comments $comment) echo $model->id == $comment->thread->id; // 1
p.s. sorry english, know it's bad.
you need define rule in comment
model:
public function relations() { homecoming array( 'thread'=>array(self::belongs_to, 'thread', 'thread_id'), );
}
this means each comment belongs 1 thread. now, structure, can following:
$comments = comment::model()->with("thread")->findall(); foreach($comments $comment) ...
php yii parent relation
Comments
Post a Comment