コード例 #1
0
ファイル: DBObject.obj.php プロジェクト: jrgns/backend-php
 public function getSelectSQL($options = array())
 {
     //Check the DB Connection
     $this->error_msg = false;
     if (!$this->checkConnection()) {
         if (class_exists('BackendError', false)) {
             BackendError::add(get_class($this) . ': DB Connection Error', 'getSelectSQL');
         }
         $this->error_msg = 'DB Connection Error';
         return false;
     }
     $mode = array_key_exists('mode', $options) ? $options['mode'] : 'list';
     $query = new SelectQuery($this, array('connection' => $this->db));
     //Fields
     $fields = array_key_exists('fields', $options) ? $options['fields'] : array();
     if (empty($fields)) {
         $query->field("`{$this->meta['table']}`.*");
     } else {
         $query->field($fields);
     }
     //Joins
     $joins = array_key_exists('joins', $options) ? $options['joins'] : array();
     if (count($joins)) {
         foreach ($joins as $join) {
             if (is_array($join)) {
                 $query->joinArray($join);
             }
         }
     }
     $q_params = array();
     if (!empty($options['conditions'])) {
         $query->filter($options['conditions']);
     }
     //Mode specific
     $limit = false;
     switch ($mode) {
         case 'object':
         case 'array':
         case 'full_object':
             if (!empty($this->meta['id'])) {
                 $query->filter("`{$this->meta['table']}`.`{$this->meta['id_field']}` = :{$this->meta['table']}_id");
                 $q_params[":{$this->meta['table']}_id"] = $this->meta['id'];
             } else {
                 $query->limit(empty($limit) ? 1 : $limit);
             }
             break;
         case 'list':
             if (array_key_exists('limit', $options) && $options['limit'] != 'all') {
                 $query->limit($options['limit']);
             }
             break;
     }
     //Parameters
     if (array_key_exists('parameters', $options)) {
         if (is_array($options['parameters'])) {
             $q_params = array_merge($q_params, $options['parameters']);
         } else {
             $q_params[] = $options['parameters'];
         }
     } else {
         if (!empty($this->meta['parameters'])) {
             if (is_array($this->meta['parameters'])) {
                 $q_params = array_merge($q_params, $this->meta['parameters']);
             } else {
                 $q_params[] = $parameters;
             }
         }
     }
     //Filters
     if (array_key_exists('filters', $options)) {
         $query->filter($options['filters']);
     } else {
         if (!empty($this->meta['filters'])) {
             $query->filter($this->meta['filters']);
         }
     }
     //Order
     if (array_key_exists('order', $options)) {
         $query->order($options['order']);
     } else {
         if (!empty($this->meta['order'])) {
             $query->order($this->meta['order']);
         }
     }
     //Group
     if (array_key_exists('group', $options)) {
         $query->group($options['group']);
     } else {
         if (!empty($this->meta['group'])) {
             $query->group($this->meta['group']);
         }
     }
     //Check Ownership
     if (array_key_exists('owner_id', $this->meta['fields'])) {
         if ($user = BackendUser::check()) {
             if (!in_array('superadmin', $user->roles)) {
                 $query->filter("`{$this->meta['table']}`.`owner_id` = :owner_id");
                 $q_params[':owner_id'] = $user->id;
             }
         }
     }
     return array($query, $q_params);
 }