Re: Adding a new counter cache
If you want a simple CakePHP only solution: http://www.dereuromark.de/2013/10/29/resetbehavior-and-hazardablebehavior/ you simply create a shell command "counts" or sth and add those 2 lines to re-save all records in a loop $this->Post->Behaviors->load('Tools.Reset', array('fields' => array(...))); $this->Post->resetRecords(); the countercache should update itself - done :) Am Montag, 15. März 2010 19:16:56 UTC+1 schrieb Brenda: > > I'm upgrading an existing model to include counters for some of the > hasMany relationships. They all start out as zero, even though there > are many hasMany records; each record gets updated once a save/delete > occurs. > > Is there any way to tell Cake to go through and update all the > counters, so I can get correct initial values? Or should I just write > a SQL query to do it? > > thanks... > > -- 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+unsubscr...@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/groups/opt_out.
Re: Adding a new counter cache
No .. It will only run through the table once updating all records. On 23 Jan 2014 12:34, "Aurelian Apostol" wrote: > Wouldn't this run an infinte loop? It needs a further WHERE clause maybe > where model_a.model_b_count IS NOT NULL > > marți, 16 martie 2010, 10:28:28 UTC+2, phpMagpie a scris: >> >> I would also run a SQl query direct on the database using phpMyAdmin >> or the CLI >> >> UPDATE model_a SET model_a.model_b_count = (SELECT COUNT(model_b.id) >> FROM model_b WHERE model_b.model_a_id = model_a.id) >> >> HTH >> >> Paul >> >> -- > 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 a topic in the > Google Groups "CakePHP" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/cake-php/J6qKv8EBLMY/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > cake-php+unsubscr...@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/groups/opt_out. > -- 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+unsubscr...@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/groups/opt_out.
Re: Adding a new counter cache
Wouldn't this run an infinte loop? It needs a further WHERE clause maybe where model_a.model_b_count IS NOT NULL marți, 16 martie 2010, 10:28:28 UTC+2, phpMagpie a scris: > > I would also run a SQl query direct on the database using phpMyAdmin > or the CLI > > UPDATE model_a SET model_a.model_b_count = (SELECT COUNT(model_b.id) > FROM model_b WHERE model_b.model_a_id = model_a.id) > > HTH > > Paul > > -- 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+unsubscr...@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/groups/opt_out.
Re: Adding a new counter cache
Nice idea, thanks for that :) Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: Adding a new counter cache
Oh, just for grins, decided to try and automate this. I have a Configurations controller where I do some admin stuff for the site, and I added this function: function counterCheck( ) { $counterTemplate = "UPDATE :atable AS :amodel SET :counter = ( SELECT COUNT(:model.id) FROM :table AS :model WHERE :model.:foreignkey = :amodel.id )"; $updates = array(); $models = Configure::listObjects('model') ; foreach ($models as $model) { $this->loadModel($model); $mainTable = Inflector::tableize($model); if (!empty($this->$model->belongsTo)) { foreach ($this->$model->belongsTo as $assocModel => $def) { if (isset($def['counterCache']) && $def['counterCache']) { $assocTable = Inflector::tableize($def['className']); $assocClass = $def['className']; $foreignKey = $def['foreignKey']; $counter = strtolower($model) . '_count'; $query = String::insert($counterTemplate, array( 'table' => $mainTable, 'model' => $model, 'atable' => $assocTable, 'amodel' => $assocModel, 'foreignkey' => $foreignKey, 'counter' => $counter )); $result = $this->Configuration->query($query); $affectedRows = $this->Configuration- >getAffectedRows(); $updates[] = array( 'query' => $query, 'affected_rows' => $affectedRows ); } } } } $this->set('updates', $updates); } // end function Counter checks The associated view just dumps the results in a table to display. Seems to work to update the counters. The affectedRows lets me know how many rows had their values changed, meaning something went wrong somewhere in the database. Or, as is more likely the case since I'm still developing this, I need to update the counters because I'm still mucking around in the database some. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: Adding a new counter cache
Thanks everyone. I'll go the SQL route. Just wanted to make sure I wasn't missing something obvious! ...Brenda On Mar 16, 4:28 am, WebbedIT wrote: > I would also run a SQl query direct on the database using phpMyAdmin > or the CLI > > UPDATE model_a SET model_a.model_b_count = (SELECT COUNT(model_b.id) > FROM model_b WHERE model_b.model_a_id = model_a.id) > > HTH > > Paul Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: Adding a new counter cache
I would also run a SQl query direct on the database using phpMyAdmin or the CLI UPDATE model_a SET model_a.model_b_count = (SELECT COUNT(model_b.id) FROM model_b WHERE model_b.model_a_id = model_a.id) HTH Paul Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: Adding a new counter cache
Your second option sounds correct for me! No need to mess around with PHP when the database and a "little" script can handle it! At least I would use SQL for it :) Enjoy, John On Mar 15, 8:16 pm, Brenda wrote: > I'm upgrading an existing model to include counters for some of the > hasMany relationships. They all start out as zero, even though there > are many hasMany records; each record gets updated once a save/delete > occurs. > > Is there any way to tell Cake to go through and update all the > counters, so I can get correct initial values? Or should I just write > a SQL query to do it? > > thanks... Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en