/** * Returns the counters. The possible options are : * * limit : maximal number of counters to be returned * * order : asc or desc, based on the counter field. * * @param Criteria $c * @param array $options */ public static function getCounters(Criteria $c = null, $options = array()) { if ($c == null) { $c = new Criteria(); } if (isset($options['limit'])) { $c->setLimit($options['limit']); } if (isset($options['order'])) { if ($options['order'] == 'asc') { $c->addAscendingOrderByColumn(sfCounterPeer::COUNTER); } else { $c->addDescendingOrderByColumn(sfCounterPeer::COUNTER); } } return sfCounterPeer::doSelect($c); }
/** * */ public function executeMostViewed() { $this->objects = sfCounterPeer::getMostCounted(); }
public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = sfCounterPeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setID($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setCountableModel($arr[$keys[1]]); } if (array_key_exists($keys[2], $arr)) { $this->setCountableId($arr[$keys[2]]); } if (array_key_exists($keys[3], $arr)) { $this->setCOUNTER($arr[$keys[3]]); } }
/** * Saves the counter into the database. * * @param BaseObject $object */ public function saveCounter(BaseObject $object) { if (isset($object->_counter) && isset($object->_forced) && $object->_forced) { $c = new Criteria(); $c->add(sfCounterPeer::COUNTABLE_ID, $object->getPrimaryKey()); $c->add(sfCounterPeer::COUNTABLE_MODEL, get_class($object)); $counter = sfCounterPeer::doSelectOne($c); if (is_null($counter)) { $counter = $object->_counter; } else { $counter->setCounter($object->_counter->getCounter()); } $object->_counter = $counter; $object->_forced = false; } $object->_counter->save(); }
public static function retrieveByPKs($pks, $con = null) { if ($con === null) { $con = Propel::getConnection(self::DATABASE_NAME); } $objs = null; if (empty($pks)) { $objs = array(); } else { $criteria = new Criteria(); $criteria->add(sfCounterPeer::ID, $pks, Criteria::IN); $objs = sfCounterPeer::doSelect($criteria, $con); } return $objs; }
$object3 = _create_object(); $object3->save(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $object3->incrementCounter(); $most_counted = sfCounterPeer::getMostCounted(); $t->ok($most_counted[0]->getCounter() > $most_counted[1]->getCounter(), 'sfCounterPeer::getMostCounted() returns objects from the most counted to the less one.'); $object3->incrementCounter(); $most_counted[0]->incrementCounter(); $most_counted[0]->incrementCounter(); $t->ok(sfCounterPeer::doCount(new Criteria()) == 2, 'sfCounterPeer::getMostCounted() does not duplicate the counters, even while preloading them into the objects.'); // several instances of the same countable object have separate counters // lifecycles, once the counter has been selected from the DB. $t->ok($object3->getCounter() == 12 && $most_counted[0]->getCounter() == 13, 'several instances of the same object have separate counter\'s lifes.'); // test object creation function _create_object() { $classname = TEST_CLASS; if (!class_exists($classname)) { throw new Exception(sprintf('Unknow class "%s"', $classname)); } return new $classname(); }