error in relationships


First of all I should say that I’m new to yii

I wanna know how can I make a connection between 2 table

The thing that I wanna do is that

I have 2 tables named post and comment

-- Table structure for table `comment`


CREATE TABLE `comment` (

  `id` int(10) unsigned NOT NULL,

  `commentBody` varchar(255) NOT NULL,

  `postId` int(11) NOT NULL,

  PRIMARY KEY  (`id`),

  KEY `pk_postId` (`postId`)


-- ---------------------------------------------------------- 

-- Table structure for table `post`



  `id` int(10) unsigned NOT NULL auto_increment,

  `title` varchar(255) NOT NULL,

  `body` text NOT NULL,

  PRIMARY KEY  (`id`)


In model / post

public function relations() {

	return array(




And in model / comment

public function relations() {

	return array(




in controllers / SiteController

class SiteController extends CController


	public function actionIndex()


		$data =array();

		$data['items']= post::model()->with('comments')->findAll();



At last this is my view/site/index.php

<?php foreach($data['items'] as $d): ?>

<h2><?php echo $d['title']; ?></h2>

<p><?php echo $d['body']; ?></p>

<p><?php echo $d['postId']; ?></p>

but when I’m running the script I get this error



Property "Post.postId" is not defined.

Source File


Try $d->comments->postId in the view.


Sorry, HAS_MANY it is. Iterate over comments or $d->comments[$n]->postId.


is my way a good way to make a relationship between posts and comments ?

could you show me an example ?


Relationship declaration syntax

Note ‘className’ and ‘foreignKey’.

You can use ‘Post’ as primary model (with the ‘comments’ relationship)…

// relationship declaration in Post model


// controller

$data= Post::model()->with('comments')->findAll();


// 'index' view

foreach($models as $model)


  echo $model->title;

  echo $model->body;

  foreach ($model->comments as $comment)

    echo $comment->postId;


…or the other way around i.e ‘Comment’ as primary (with the ‘post’ relationship).

// relationship declaration in Comment model


// controller

$data= Comment::model()->with('post')->findAll();


// 'index' view

foreach($models as $model)


  echo $model->post->title;

  echo $model->post->body;

  echo $model->postId;



// relationship declaration in Post model'comments'=>array(self::HAS_MANY,'Comment','postId')// controller$data= Post::model()->with('comments')->findAll();$this->render('index',array('models'=>$data));// 'index' viewforeach($models as $model){  echo $model->title;  echo $model->body;  foreach ($model->comments as $comment)    echo $comment->postId;}

it returns null

why ?

Which statement?

Did you put the code snippets in the model, controller and view files, respectively?


Couple things starting out here… The relationships don’t look exactly right. They should look like this:



'comments'=>array(self::HAS_MANY, 'comment', 'postId'),



'post'=>array(self::BELONGS_TO, 'post', 'postId'),

Also, you don’t need to worry about eager loading as you had it…

$data['items']= post::model()->with('comments')->findAll();

Try something like this…

$data['items'] = post::model()->findAll();

foreach($items as $post){

  foreach($item->comments as $comment){/*...*/}