我在Yii中构建一个简单的民意调查应用程序时遇到了一些麻烦。
我有以下表格:
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
);
我有一个ActiveRecord for Poll定义为:
class Poll extends CActiveRecord
{
...
public function relations()
{
return array(
'choices'=>array(self::HAS_MANY, 'Choice', 'poll_id'),
);
}
...
}
但是,当我使用以下代码时:
$p = Poll::model()->findByPk($id)->with('choices')->findAll();
它给了我追溯:
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): CActiveFinder->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()
任何人都知道我做错了什么?我是Yii的新手
谢谢
答案 0 :(得分:14)
好的,我发现了问题。
民意调查表需要一个主键。