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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -