Skip to content

Commit

Permalink
Improved test in afterFind
Browse files Browse the repository at this point in the history
Improved same model findRelated()
  • Loading branch information
Pierre-Emmanuel Fringant committed Apr 17, 2009
1 parent b91f0cf commit e502aa0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions models/behaviors/taggable.php
Expand Up @@ -63,7 +63,7 @@ function afterSave(&$model)
*/
function afterFind(&$model, $results, $primary = false)
{
if(count($results) == 1 && isset($results[0][$model->alias]))
if(count($results) == 1 && isset($results[0][$model->alias][$model->primaryKey]))
{
$tags = $this->Tagged->findTags(
$model->alias,
Expand Down Expand Up @@ -189,19 +189,21 @@ function findRelated(&$model, $id = null, $restrict_to_model = true, $limit = nu
}

// Final results
$results = array();

if($restrict_to_model)
{
foreach($related as $row)
{
$model->id = $row['Tagged']['model_id'];

$results[] = $model->read();
}
$model_ids = Set::extract('/Tagged/model_id', $related);

$pk = $model->escapeField($model->primaryKey);

$conditions = array($pk => $model_ids);
$order = "FIELD({$pk}, " . join(', ', $model_ids) . ")";

$results = $model->find('all', compact('conditions', 'order'));
}
else
{
$results = array();

foreach($related as $row)
{
if($assoc_model = ClassRegistry::init($row['Tagged']['model']))
Expand Down
4 changes: 2 additions & 2 deletions models/tagged.php
Expand Up @@ -114,8 +114,8 @@ function taggedWith($model = null, $tag_ids = array(), $exclude_id = null, $limi
$conditions['id !='] = $exclude_id;
}

$fields = array('model', 'model_id', 'COUNT(tag_id) as count');
$group = 'tag_id';
$fields = array('model', 'model_id', 'COUNT(*) as count');
$group = array('model', 'model_id');
$order = 'count DESC';
$recursive = -1;

Expand Down

0 comments on commit e502aa0

Please sign in to comment.