开发者

invalid yii relation HAS_MANY

i am having some throuble building a simple Poll application in Yii.

I have the following tables:

create table poll (
    id integer not null auto_increment,
    title varchar(255) not null,
    views integer not null default 0,
    created_at timestamp not null default NOW(),
    PRIMARY KEY(id)
);

create table choice (
    poll_id integer not null,
    choice varchar(200) not null,
    votes integer not null default 0
);

I have an ActiveRecord for Poll defined as:

class Poll extends CActiveRecord
{
...
    public function relations()
    {
        return array(
            'choices'=>array(self::HAS_MANY, 'Choice', 'poll_id'),
        );
    }
...
}

However when I use the following code:

$p = Poll::model()->findByPk($id)->with('choices')->findAll();

It gives me the traceback:

Invalid argument supplied for foreach()

#0  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveFinder.php(791): CJoinElement->populateRecord(CJoinQuery, array("1", "0", "", "2011-02-28 13:11:41", ...))
#1  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveFinder.php(736): CJoinElement->populateRecord(CJoinQuery, array("1", "0", "", "2011-02-28 13:11:41", ...))
#2  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveFinder.php(395): CJoinElement->runQuery(CJoinQuery)
#3  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveFinder.php(72): CJoinElement->find(CDbCriteria)
#4  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveRecord.php(1242): CActiveFinde开发者_如何学Pythonr->query(CDbCriteria, true)
#5  
+  /home/william/scm/bmbraga/clickpoll/yii/framework/db/ar/CActiveRecord.php(1323): CActiveRecord->query(CDbCriteria, true)
#6  
+  /home/william/scm/bmbraga/clickpoll/poll/protected/controllers/PollController.php(156): CActiveRecord->findAll()

Anyone have any idea what i have been doing wrong? I am quite new to Yii

Thank you


Ok, i found out the problem.

The poll table needs a primary key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜