无效的 yii 关系 HAS_MANY

发布于 2024-10-20 07:53:06 字数 1905 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-10-27 07:53:06

好吧,我发现了问题。

轮询表需要主键。

Ok, i found out the problem.

The poll table needs a primary key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文