无法引用 Yii 的 ActiveRecord 中的相关表

发布于 2025-01-03 11:12:01 字数 707 浏览 0 评论 0原文

我试图在 Yii 中获取相关表,但在尝试引用 event.text 字段时它一直给我一个错误。

return new CActiveDataProvider( 'InvitationCode', array(
    'criteria'=>array(
    'select' => array( 't.id', 'code', 'Event.text' ),
    'condition'=>'t.id >= :min_code_id AND t.id <= :max_code_id',
    'order'=>'t.id Asc',
    'with' => array( 'Event' ),
    'params' => array(
          ':min_code_id' => $min_code_id, 
          ':max_code_id' => $max_code_id,
        ),                  
    ),      
));  

通过关系“Event”与 InvitationCode 相关的表事件确实有一个文本字段,但我无法使用“event.text”或“Event.text”访问它
如果我保留“选择”部分,它只会带来 InvitationCode 的字段,而不会带来相关表事件中的任何字段,尽管它位于“with”部分中。

如何引用相关表Event?

I'm trying to get a related table in Yii and it keeps giving me an error trying to reference the event.text field.

return new CActiveDataProvider( 'InvitationCode', array(
    'criteria'=>array(
    'select' => array( 't.id', 'code', 'Event.text' ),
    'condition'=>'t.id >= :min_code_id AND t.id <= :max_code_id',
    'order'=>'t.id Asc',
    'with' => array( 'Event' ),
    'params' => array(
          ':min_code_id' => $min_code_id, 
          ':max_code_id' => $max_code_id,
        ),                  
    ),      
));  

The table event which is related to InvitationCode through the relation 'Event' does have a text field, but I can't access it with either 'event.text' nor 'Event.text'
If I leave the 'select' part out, it only brings the fileds of InvitationCode and none of the fields from the related table Event despite it's in the 'with' part.

How can I reference the related table Event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心奴独伤 2025-01-10 11:12:01

确切的错误消息是什么?
您可以发布 InvitationCode 模型类中定义关系()的代码吗?
您是否启用了查询日志记录以查看生成的确切查询?

正如评论所指出的,您可能必须显式设置 Together=>true 才能进行急切加载。

来自: http://www.yiiframework.com/doc/guide/1.1 /en/database.arr

一起:与此关系关联的表是否应该
被迫与主表和其他表连接在一起。
此选项仅对 HAS_MANY 和 MANY_MANY 关系有意义。
如果该选项设置为 false,则与 HAS_MANY 或关联的表
MANY_MANY 关系将在单独的表中与主表连接
SQL查询,这可以提高整体查询性能,因为更少
返回重复的数据。如果此选项设置为 true,则
关联表将始终与主表连接
单个 SQL 查询,即使主表已分页。如果这个
未设置选项,关联表将与
仅当主表不是时,才在单个 SQL 查询中使用主表
分页。详细内容请参见“关系查询”部分
性能”。

What is the exact error message?
Can you post the code defining relations() in InvitationCode model class?
Did you enable query logging to see the exact query generated?

As the comment noted, you probably have to explicitly set together=>true for eager loading.

From: http://www.yiiframework.com/doc/guide/1.1/en/database.arr

together: whether the table associated with this relationship should
be forced to join together with the primary table and other tables.
This option is only meaningful for HAS_MANY and MANY_MANY relations.
If this option is set false, the table associated with the HAS_MANY or
MANY_MANY relation will be joined with the primary table in a separate
SQL query, which may improve the overall query performance since less
duplicated data is returned. If this option is set true, the
associated table will always be joined with the primary table in a
single SQL query, even if the primary table is paginated. If this
option is not set, the associated table will be joined with the
primary table in a single SQL query only when the primary table is not
paginated. For more details, see the section "Relational Query
Performance".

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