无效的 yii 关系 HAS_MANY
我在 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 定义为:
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 很陌生,
谢谢
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): 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()
Anyone have any idea what i have been doing wrong? I am quite new to Yii
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我发现了问题。
轮询表需要主键。
Ok, i found out the problem.
The poll table needs a primary key.