gpt4 book ai didi

yii - 无效的 yii 关系 HAS_MANY

转载 作者:行者123 更新时间:2023-12-04 21:49:59 25 4
gpt4 key购买 nike

我在用 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 很陌生

谢谢

最佳答案

好的,我发现了问题。

轮询表需要一个主键。

关于yii - 无效的 yii 关系 HAS_MANY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5167306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com