如何获取 cakePHP Model->field 方法来使用关联模型的条件?
我有一个问题,我需要从单个记录中检索单个字段,没有关联模型,同时使用使用关联模型的条件?
示例:
Categories {name, id}
Keywords {name, id}
Articles {title, text, id, keyword_id, category_id}
我想在 Article.category_id=3
等时检索第一个 Articles.id
。
使用 Model->field('model.field',array只要条件不使用外部模型,(conditions))
就可以工作。 ($this->Model->recursive=4
不起作用)。
使用 Model->find('first', array(conditions)) 效果很好,除了我还获得了我不需要且不想要的关联数据,从而限制了递归结果禁用了使用关联模型比较的能力。
有什么建议吗?
编辑
我的问题,除了糟糕的调试器:-)是通过使用 model->read
方法时限制递归来解决的。 然而,从长远来看,可能是使用可控制的行为。
I have a problem, I need to retrieve a single field from a single record, without the associated models, while using conditions that uses the associated models?
Example:
Categories {name, id}
Keywords {name, id}
Articles {title, text, id, keyword_id, category_id}
I want to retrieve the first Articles.id
when Article.category_id=3
etc.
Using Model->field('model.field',array(conditions))
is working as long the conditions are not using the outer models. ($this->Model->recursive=4
isn't working).
Using Model->find('first', array(conditions))
works fine, except the fact that I get also the associated data that I don't need and don't want, limiting the recursion results with disabling the ability to use the associated models comparison..
any advice?
edit
my problem, except from being poor debugger :-) was solved by limiting the recursion while using the model->read
method.
however, the long term is, probably, using the containable behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
find() 方法采用
字段
参数作为其选项之一。您还可以设置recursive
参数以确保您获得适当级别的关系。根据您的OP,以下内容将检索您想要的内容,而无需加入任何关系。
如果你的要求比上面更强,你可以看看绑定模型 或 可控制的行为。但根据您的描述,以上是合适的解决方案。在我看来,这些都太过分了。
The find() method takes a
fields
argument as one of its options. You can also set therecursive
argument to ensure you are getting the appropriate level of relationships.Based on your OP, the following will retrieve what you want without joining any relationships.
If your requirements are stronger than the above, you can look at binding models or Containable Behavior. But from what you described, the above is the appropriate solution. These would be overkill IMO.
我将使用
Model->find()
和 containable 行为用于限制加载哪些关联,而 field 参数用于将结果限制为所需的字段。I would use
Model->find()
with the containable behavior to limit what associations are loaded and the field argument to limit the results only to the desired field.