CakePHP:限制与模型关联的字段 [英] CakePHP: Limit Fields associated with a model

查看:149
本文介绍了CakePHP:限制与模型关联的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些数据库表中有几个字段,我的CakePHP模型从来不需要检索。有没有一些方法来设置默认的字段集在模型级别提取?例如,我从第三方设计的数据库检索一些数据,每个表有50个字段,我使用5。



我知道我可以设置字段的限制find()查询和模型之间的任何关联时,但我想知道是否有一个模型级方法。

解决方案

CakePHP不提供您在模型级别开箱即用描述的内容。也就是说,没有 defaultFields 的Model属性用于每个 find()



正如你所说,你可以通过设置 fields 属性来在关联级别指定。但是,这只有在您通过这些关系之一检索模型时才会工作。



最后,您将在 find()。您可以通过向模型中添加属性来最小化重复自己:

  var $ defaultFields = array('Model.field1' ,'Model.field2',...);然后在 find()   / p> 

  $ this-> Model-> find('fields'=> $ this-> Model-> defaultFields ,...); 

这有明显的限制,但至少提供了一些封装,因此灵活性。



注意 beforeFind(); 。在这种情况下,您不需要调整每个 find()。但是,您的里程可能会根据您的使用情况而有所不同。


I have several fields in some of my database tables that my CakePHP models never need to retrieve. Is there some way to set a default set of fields to fetch at the model level? For instance I retrieve some data from a third party designed database that has 50 fields per table, I use 5.

I know I can set limits on fields at the time of the find() query and at the time of any associations between models, but I was wondering if there was a model-level approach.

解决方案

CakePHP does not offer what you describe at the Model level out of the box. That is to say there is no Model property of defaultFields that is used on every find()

As you noted, you could specify this at the association level by setting the fields property. However, this would only work when you were retrieving the Model across one of these relationships.

In the end, you're going to be setting this in your find(). You could minimize repeating yourself by adding a property to your model like so:

var $defaultFields = array('Model.field1', 'Model.field2', ...);

Then in your find():

$this->Model->find('fields' => $this->Model->defaultFields, ...);

This has obvious limitations, but at least provides some encapsulation and therefore flexibility.

Note: A more invasive approach could use beforeFind();. In which case you would not need to adjust every find(). But your mileage may vary based on your usage.

这篇关于CakePHP:限制与模型关联的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆