Development

Changeset 5774

You must first sign up to be able to contribute.

Changeset 5774

Show
Ignore:
Timestamp:
10/31/07 09:58:29 (1 year ago)
Author:
xavier
Message:

getNbComments() optimization

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelActAsCommentableBehaviorPlugin/trunk/lib/sfPropelActAsCommentableBehavior.class.php

    r5664 r5774  
    111111  public function getComments(BaseObject $object, $options = array(), Criteria $criteria = null) 
    112112  { 
     113    $c = $this->getCommentsCriteria($object, $options, $criteria); 
     114    $comment_objects = sfCommentPeer::doSelect($c); 
     115    $comments = array(); 
     116 
     117    foreach ($comment_objects as $comment_object) 
     118    { 
     119      $comment = $comment_object->toArray(); 
     120      $comments[] = $comment; 
     121    } 
     122 
     123    return $comments; 
     124  } 
     125 
     126  /** 
     127   * Returns a criteria for comments selection. The options array 
     128   * can contain several options : 
     129   * - order : order of the comments 
     130   *  
     131   * @param      BaseObject  $object 
     132   * @param      Array       $options 
     133   * @param      Criteria    $criteria 
     134   *  
     135   * @return     Array 
     136   */ 
     137  protected function getCommentsCriteria(BaseObject $object, $options = array(), Criteria $criteria = null) 
     138  { 
    113139    if ($criteria != null) 
    114140    { 
     
    143169    } 
    144170 
    145     $comment_objects = sfCommentPeer::doSelect($c); 
    146     $comments = array(); 
    147  
    148     foreach ($comment_objects as $comment_object) 
    149     { 
    150       $comment = $comment_object->toArray(); 
    151       $comments[] = $comment; 
    152     } 
    153  
    154     return $comments; 
     171    return $c; 
    155172  } 
    156173 
     
    166183  public function getNbComments(BaseObject $object, $options = array(), Criteria $criteria = null) 
    167184  { 
    168     return count($this->getComments($object, $options, $criteria)); 
     185    $c = $this->getCommentsCriteria($object, $options, $criteria); 
     186    return sfCommentPeer::doCount($c); 
    169187  } 
    170188 
  • plugins/sfPropelActAsCommentableBehaviorPlugin/trunk/test/unit/sfPropelActAsCommentableBehaviorTest.php

    r5680 r5774  
    2121 
    2222// start tests 
    23 $t = new lime_test(8, new lime_output_color()); 
     23$t = new lime_test(11, new lime_output_color()); 
    2424 
    2525 
     
    4545$object2_comments = $object2->getComments(); 
    4646$t->ok((count($object1_comments) == 2) && (count($object2_comments) == 1), 'one comment is only attached to one Propel object.'); 
     47$t->ok(count($object1_comments) == $object1->getNbComments(), 'getNbComment() permùits to retrieve one object\'s comments number.'); 
    4748 
    4849$object3 = _create_object();