Example #1
0
 /**
  * Override __call magic method to provide many helper methods
  * 
  * @param string $name
  * @param array $arguments
  * @return mixed
  */
 public function __call($name, $arguments)
 {
     //findOneByXXX/findManyByXXX
     if (preg_match('#^find(One|Many)By(.+)$#', $name, $matches)) {
         $one = $matches[1] === 'One';
         $findByKey = Model::entityNameToDBName($matches[2]);
         array_unshift($arguments, $findByKey);
         if ($one) {
             return call_user_func_array(array($this, 'findOneBy'), $arguments);
         } else {
             return call_user_func_array(array($this, 'findManyBy'), $arguments);
         }
     } elseif (preg_match('#^(update|delete|count)By(.+)$#', $name, $matches)) {
         $action = $matches[1];
         $actionByKey = Model::entityNameToDBName($matches[2]);
         array_unshift($arguments, $actionByKey);
         return call_user_func_array(array($this, $action . 'By'), $arguments);
     } else {
         throw new Exception(sprintf('Helper method "%s" does not exist', $name));
     }
 }