error in relationships

Hi,there

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`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


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

-- Table structure for table `post`

-- 


CREATE TABLE `post` (

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

  `title` varchar(255) NOT NULL,

  `body` text NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;



In model / post




public function relations() {

	return array(

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

	);

}




And in model / comment




public function relations() {

	return array(

		'posts'=>array(self::BELONGS_TO,'comment','postId')

	);

}



in controllers / SiteController




class SiteController extends CController

}	

	public function actionIndex()

	}		

		$data =array();

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

		$this->render('index',array('data'=>$data((;

	{






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

CException

Description

Property "Post.postId" is not defined.

Source File

D:\wamp\www\yii\framework\db\ar\CActiveRecord.php(107)

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

Edit:

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

/Tommy

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

could you show me an example ?

thanks

Relationship declaration syntax

Note ‘className’ and ‘foreignKey’.

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




// 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' 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

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


// controller

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

$this->render('index',array('models'=>$data));


// 'index' view

foreach($models as $model)

{

  echo $model->post->title;

  echo $model->post->body;

  echo $model->postId;

}



/Tommy


// 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?

/Tommy

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




//models/Post

//relations

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


//models/Comment

//relations

'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){/*...*/}

}