Beispiel #1
0
 /**
  * @param string|object $class_name_or_obj
  * @param array $params
  * @param lmbDbConnection $conn
  * @param string $sql
  * @return lmbARQuery
  */
 static function create($class_name_or_obj, $params = array(), $conn = null, $sql = '')
 {
     if (!$conn) {
         $conn = lmbToolkit::instance()->getDefaultDbConnection();
     }
     if (!is_object($class_name_or_obj)) {
         $class_name_or_obj = new $class_name_or_obj();
     }
     $query = new lmbARQuery($class_name_or_obj, $conn, $sql, $params);
     if (isset($params['criteria']) && $params['criteria']) {
         $criteria = lmbSQLCriteria::objectify($params['criteria']);
     } else {
         $criteria = lmbSQLCriteria::create();
     }
     $has_class_criteria = false;
     if (isset($params['class'])) {
         $filter_object = new $params['class']();
         $criteria = $filter_object->addClassCriteria($criteria);
         $has_class_criteria = true;
     }
     if (!$has_class_criteria) {
         $criteria = $class_name_or_obj->addClassCriteria($criteria);
     }
     $query->where($criteria);
     $sort_params = isset($params['sort']) && $params['sort'] ? $params['sort'] : $class_name_or_obj->getDefaultSortParams();
     $query->order($sort_params);
     if (isset($params['group']) && $params['group']) {
         $query->group($params['group']);
     }
     $join = isset($params['join']) && $params['join'] ? $params['join'] : array();
     if (!is_array($join)) {
         $join = explode(',', $join);
     }
     foreach ($join as $relation_name => $params_or_relation_name) {
         if (is_numeric($relation_name)) {
             $query->eagerJoin(trim($params_or_relation_name));
         } else {
             $query->eagerJoin(trim($relation_name), $params_or_relation_name);
         }
     }
     $attach = isset($params['attach']) && $params['attach'] ? $params['attach'] : array();
     if (!is_array($attach)) {
         $attach = explode(',', $attach);
     }
     foreach ($attach as $relation_name => $params_or_relation_name) {
         if (is_numeric($relation_name)) {
             $query->eagerAttach(trim($params_or_relation_name));
         } else {
             $query->eagerAttach(trim($relation_name), $params_or_relation_name);
         }
     }
     return $query;
 }
 function testEmptyCriteriaChainingIsSafe()
 {
     $c = lmbSQLCriteria::create()->add(new lmbSQLCriteria());
     $this->assertEqual($c->toStatementString(), '1 = 1 AND 1 = 1');
 }