Monday, March 24, 2014

RE: $this->field and contain

I was under the impression counterCache holds record counts thru the relationships.

 

I use that when a user has x number of related model records.

In this case it's a page view count that simply updates when viewed, no relation to anything else there is no User hasMany PageView relationship.

 

User hasMany Post, Post is viewed, update the view count of that Post.

User counterCache for number of Post for that user but the Post does not 150 PageView records.

 

Am I missing something?

 

 

From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of John Sposato
Sent: Monday, March 24, 2014 12:42 PM
To: cake-php@googlegroups.com
Subject: Re: $this->field and contain

 

Any reason you can't use counterCache?



On Monday, March 24, 2014 11:07:46 AM UTC-4, advantage+ wrote:

Can you use / is there a way to read a field and not add in extra relation queries?

 

I have a very simple function to read 1 field and update it by +1

 

public function pageCount($id) {

                $this->id = $id;

                $count = $this->field('view_count', array('id' => $id)) + 1;

                $this->saveField('view_count', $count);

}

 

Right now the full page has 30 querys exectuted.
Removing this call from the controller.

 

$this->User->pageCount($user['User']['id']);

 

And the page queries drop to 23. Odd how a simple call to read 1 field and update by 1 creates 7 queries.

Looking at the extra queries its pulling in related models.

 

I can write a find and use contain => false I suppose but figured I would ask.

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: