Exemple #1
0
 /**
  * Constructor.
  *
  * Provide a Query object to run this query and return found objects.
  * Provide an integer to look for this ID (or $column).
  * Provide an array to create an new object filled with these values.
  *
  * @param mixed $mixed  What to get?
  * @param mixed $column Column to search in.
  *
  * @return void
  */
 public function __construct($mixed = null, $column = 'id')
 {
     //set modelname
     $this->modelName = get_called_class();
     //set table name
     if (empty($this->tableName)) {
         $this->tableName = Config::getSetting('db_table_prefix') . strtolower($this->modelName);
     }
     //cache fields
     parent::__construct();
     //determine action
     if ($mixed !== null) {
         if (is_object($mixed) && get_class($mixed) == 'Query' && Clockwork::isModuleLoaded('Data/Query')) {
             if ($column == 1) {
                 $this->values = $mixed->from(substr($this->tableName, strlen(Config::getSetting('db_table_prefix'))), null, true, true)->limit(1)->run(1);
                 if (empty($this->values)) {
                     $this->setError(404, 'Object not found');
                 }
             } else {
                 $this->objects = $this->createObjects($mixed->from(substr($this->tableName, strlen(Config::getSetting('db_table_prefix'))), null, true, true)->run());
             }
         } else {
             if (is_array($mixed)) {
                 foreach ($mixed as $key => $value) {
                     $this->set($key, $value);
                 }
             } else {
                 if (Clockwork::isModuleLoaded('Data/Query')) {
                     $query = new Query();
                     $this->values = $query->from(strtolower($this->modelName))->where($column . " = '" . $mixed . "'")->run(1);
                     if (empty($this->values)) {
                         $this->setError(404, 'Object not found');
                     }
                 }
             }
         }
     }
 }