Пример #1
0
 /**
  * Find count of entities by where conditions.
  * All where conditions applied with AND
  *
  * @param \Modler\Model $model Model instance
  * @param array $where Data to use in "where" statement
  * @return array Fetched data
  */
 public function count(\Modler\Model $model, array $where = array())
 {
     $properties = $model->getProperties();
     list($columns, $bind) = $this->setup($where);
     $update = array();
     foreach ($bind as $column => $name) {
         // See if we keep to transfer it over to a column name
         if (array_key_exists($column, $properties)) {
             $column = $properties[$column]['column'];
         }
         $update[] = $column . ' = ' . $name;
     }
     $sql = 'select count(*) as `count` from ' . $model->getTableName();
     if (!empty($update)) {
         $sql .= ' where ' . implode(' and ', $update);
     }
     $result = $this->fetch($sql, $where, true);
     return $result;
 }
Пример #2
0
 /**
  * Find records matching the "where" data given
  *     All "where" options are appended via "and"
  *
  * @param \Modler\Model $model Model instance
  * @param array $where Data to use in "where" statement
  * @param boolean $multiple Force return of single/multiple
  * @return array Fetched data
  */
 public function find(\Modler\Model $model, array $where = array(), $multiple = false)
 {
     $properties = $model->getProperties();
     list($columns, $bind) = $this->setup($where);
     $update = array();
     foreach ($bind as $column => $name) {
         // See if we keep to transfer it over to a column name
         if (array_key_exists($column, $properties)) {
             $column = $properties[$column]['column'];
         }
         $update[] = $column . ' = ' . $name;
     }
     $sql = 'select * from ' . $model->getTableName();
     if (!empty($update)) {
         $sql .= ' where ' . implode(' and ', $update);
     }
     $result = $this->fetch($sql, $where);
     if ($result !== false && count($result) == 1 && $multiple === false) {
         $model->load($result[0]);
         return $model;
     } elseif (count($result) > 1 || $multiple === true) {
         // Make a collection instead
         $modelClass = get_class($model);
         $collectionNs = str_replace('Model', 'Collection', $modelClass);
         if (!class_exists($collectionNs)) {
             throw new \InvalidArgumentException('Collection "' . $collectionNs . '" is invalid!');
         }
         $collection = new $collectionNs($this);
         foreach ($result as $item) {
             $itemModel = new $modelClass($this, $item);
             $collection->add($itemModel);
         }
         return $collection;
     }
     return $model;
 }