如何使用find方法限制关联模型的字段

编程入门 行业动态 更新时间:2024-10-27 14:32:25
本文介绍了如何使用find方法限制关联模型的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有用户模型和文章模型。一个用户有很多文章。因此,当我查询用户时,将检索文章表的所有字段。我只想限制文章标题。

I have user model and articles model. An User hasMany articles. So when I query for a user, all fields for article table is retrieved. I want to limit it just title of articles.

$user = $this->User->find('all', array('conditions' => array('User.id' => $id), 'fields' => array('User.firstName', 'Article.title')));

该字段适用于用户模型。但这不适用于关联的模型。引发错误

The fields works fine for user model. But it does not work for associated models. throws error

SQL Error: 1054: Unknown column 'Article.title' in 'field list'

感谢您的帮助。

推荐答案

最好使用可容纳,它也很简单:

You're better of using Containable, and it's just as easy:

$this->User->Behaviors->attach('Containable'); $user = $this->User->find('all', array('conditions' => array('User.id' => $id), 'contain' => array('Article.title'), 'fields' => array('User.firstName')));

更多推荐

如何使用find方法限制关联模型的字段

本文发布于:2023-11-05 21:14:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   如何使用   模型   方法   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!