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)