Example #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;
 }
Example #2
0
 /**
  * Load the given data into the current model
  *
  * @param array $data Property data
  * @return boolean True when complete
  */
 public function load(array $data, $enforceGuard = true)
 {
     $loadData = array();
     $properties = $this->getProperties();
     foreach ($properties as $propertyName => $propertyDetail) {
         if (!isset($propertyDetail['column'])) {
             if (isset($data[$propertyName])) {
                 $loadData[$propertyName] = $data[$propertyName];
             }
             continue;
         }
         $column = $propertyDetail['column'];
         if (isset($data[$column])) {
             $loadData[$propertyName] = $data[$column];
         }
     }
     parent::load($loadData, $enforceGuard);
     return true;
 }
Example #3
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;
 }
Example #4
0
 /**
  * Load the given data into the current model
  *
  * @param array $data Property data
  * @param boolean $enforceGuard Enforce guarded properties
  * @return boolean True when complete
  */
 public function load(array $data, $enforceGuard = true)
 {
     $loadData = array();
     foreach ($this->getProperties() as $propertyName => $propertyDetail) {
         // If it's a normal column
         if (isset($propertyDetail['column'])) {
             $column = $propertyDetail['column'];
             if (isset($data[$column]) || isset($data[$propertyName])) {
                 $value = isset($data[$column]) ? $data[$column] : $data[$propertyName];
                 $loadData[$propertyName] = $value;
             }
             // Or for relations...
         } elseif ($propertyDetail['type'] == 'relation') {
             if (isset($data[$propertyName])) {
                 $loadData[$propertyName] = $data[$propertyName];
             }
         }
     }
     parent::load($loadData, $enforceGuard);
     return true;
 }