mysql - CakePHP counterCache joining irrelevant tables to update counter -


I have a user model and message model.

The message model is connected to the user model twice this way:

  public $ associTo = array ('UserSender' = & gt; array ('className' = ' & Gt; 'user', 'foreign key' = & gt; 'sender_id', 'counter cache array (' class_name '= & gt; array (' message_sent_count '= & gt; array (' is_deleted '= & gt; wrong )), 'UserRecipient' = & gt; array ('className' = & gt; 'user', 'foreign key' '> array' array ('is_deleted' => array ('is_deleted' = & gt; ; Incorrect), 'messages_unread_count' = & gt; array ('is_deleted' = & gt; wrong, 'is_read' = & gt; recipient_id ',' counter cache '=> FALSE)),' operator '= & Gt; Array ('className' = & gt; 'operator', 'foreign key' = & gt; 'operator_id'));  

In addition to the user model, the message model also includes the $ related operator model The operator model is irrelevant for the message count for the user, but its table is still being included in the counting query, because debug shows:

  'query' = & gt; In the 'message' AS 'message' as 'counting' from `database` as selection number (*), join 'database'. 'Operator' in the form of 'operator' (`message '. Operator_id` =` operator`. Id`) LEFT JOIN' database '. 'User' as AS 'UserSender' (`Message '` `` `UserS`````````````` except" Database ")' Message on User Recipe '.recipient_id `=` UserRecipient```````````` where `message```_arigitted`` = '0' and 'message'` `` `` `389`, 'params' => Array (), 'affected' = & gt; (Int) 1, 'number' = & gt; (Int) 1, 'taken' => For the simple (float) 394 simplicity I have excluded another model that includes the message model $ $, but the above questions show the problem.  

The counter cache functions do a fairly expensive query to update the counter. What is a way to override or adjust the counter cache method to join in irrelevant tables in the query?

I can not test it right now, but since the conditions for the stringent cache field are defined , The only way to change this (fully model :: updateCounterCache ()) ) probably count query to model :: _ findCount () Or model: firstFind () to its message model.

  Prior to the public function ($ query) {// ... this is the data whether it is the counting query for updateCounterCache, // Perhaps try to check whether the old conditions Requires // contains or not if (/ * ... * /) {$ query ['recursive'] = -1; } Return $ query; }  > 

Depending on how much control you may need, the trick can also be set, set it to recursive to -1 No containment is being done in the case

  $ message-> (); // === Recurring callback $ message- & gt; Removing (123) before being set to 1;  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -