简体   繁体   English

CakePHP ::具有非主要模型的afterFind()

[英]CakePHP :: afterFind() with non-primary model

In this application, I have a Rental model which is central to the application. 在此应用程序中,我有一个“租赁”模型,该模型是该应用程序的核心。 Rentals are associated with a Client, a Location, and a piece of Equipment. 租金与客户,位置和设备相关联。 In almost all cases, I want to 'clean' the results, leaving only Rentals that are currently active. 在几乎所有情况下,我都想“清理”结果,只保留当前处于活动状态的租车。 However, I don't want to do that in all cases, so what is the best methodology for flagging this? 但是,我不想在所有情况下都这样做,那么标记此的最佳方法是什么?

My current thought is to add a Model variable ( $cleanRentals = true ), and set that flag to false when necessary, and to check the variable in afterFind() . 我当前的想法是添加一个Model变量( $cleanRentals = true ),并在必要时将该标志设置为false,并在afterFind()检查该变量。

Protecting / cleaning data like this on associated models in Cake shows where Cake's model system is lacking. 在Cake中关联模型上的保护/清理数据可显示出Cake模型系统所缺乏的地方。 It gets messy. 太乱了。

But I would consider creating a custom finder, something like 但我会考虑创建一个自定义查找器,例如

$this->Rental->findActive();

Then you could just use that when you want to get only the active rentals. 然后,当您只想获得活动租金时,就可以使用它。

See more: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types 查看更多: http : //book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM