简体   繁体   English

¿如果另一个相关表中不存在该记录,如何避免分页中的记录? CAKEPHP

[英]¿How to avoid record from a table in pagination if this record is not present in another related table? CAKEPHP

I have a doubt doing pagination in CakePHP. 我怀疑在CakePHP中进行分页。

Well, I will try to be concise. 好吧,我会尽量简洁。

I have three tables, one table is which I paginate (1st table), another table (2nd table) dependent of this one (1st table have FK to 2nd table), and the most important, 3rd table, that has FK to 1st table. 我有三个表,其中一个表是我分页的(第一个表),另一个表(第二个表)与此表相关(第一个表具有FK到第二个表),而最重要的第三个表具有FK到第一个表。 So: 3rd table-> 1st table-> 2nd table. 因此:第三张表->第一张表->第二张表。 I do pagination of 1st table (model) from his controller, so far everything ok, the pagination list all the records from table bd and no problems. 我从他的控制器对第一个表(模型)进行分页,到目前为止一切正常,分页列出了表bd中的所有记录,没有问题。

I need also to get records from 1st table but ordering by a record from 2nd table. 我还需要从第一张表中获取记录,但要从第二张表中的记录进行排序。 No problem. 没问题。 The problem IS : I WANT not to get in pagination, records from 1st table (which i paginate it) that arent present in 3rd table. 问题是 :我不想分页,从第1个表(我将其分页)记录在第3个表中。

The problem is that CAKEPHP first make a query getting records from 1st table + 2nd table. 问题在于,CAKEPHP首先进行查询以从第一张表+第二张表中获取记录。 After this, it get the records from 3rd table according to the records (PK) obtained from the first query, so I cant say it: not take records from 1st table that arent present in 3rd table. 此后,它根据从第一个查询获得的记录(PK)从第三张表中获取记录,因此我不能说:不从第一张表中获取第三张表中没有的记录。

¿How can I do this? 我怎样才能做到这一点?

I hope I explained. 我希望我能解释。

Thank you very much. 非常感谢你。

Regards. 问候。

what you'll want to use is a Counter Cache. 您要使用的是计数器缓存。 Go to http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html and search for 'counterCache,' and you'll find instructions. 转到http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html并搜索“ counterCache”,您将找到说明。

Basically, you want to implement a "counter cache" field in model 1, that automatically keeps count of the number of linked records in model 3. Then, to exclude records from Model 1 that have no related records in Model 3, you just add a pagination condition to say that the counter cache field must be greater than 0. 基本上,您想在模型1中实现一个“计数器缓存”字段,该字段会自动保留模型3中链接记录的数量。然后,要从模型1中排除在模型3中没有相关记录的记录,您只需添加分页条件,表示计数器缓存字段必须大于0。

UPDATE: 更新:

If you can't use CakePHP's counter cache, then you should emulate the behaviour of the counter cache using a virtual field - see http://book.cakephp.org/2.0/en/models/virtual-fields.html 如果您不能使用CakePHP的计数器缓存,则应使用虚拟字段模拟计数器缓存的行为-请参见http://book.cakephp.org/2.0/en/models/virtual-fields.html

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

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