Ejemplo n.º 1
0
 /**
  * Create a criteria for a specific model
  *
  * @param \Phalcon\DiInterface|null $dependencyInjector
  * @return \Phalcon\Mvc\Model\Criteria
  */
 public static function query($dependencyInjector = null)
 {
     if (is_object($dependencyInjector) === false || $dependencyInjector instanceof DiInterface === false) {
         $dependencyInjector = DI::getDefault();
     }
     $criteria = new Criteria();
     $criteria->setDi($dependencyInjector);
     $criteria->setModelName(get_called_class());
     return $criteria;
 }
Ejemplo n.º 2
0
 /**
  * @param array $data
  * @param string $operator
  * @return \Phalcon\Mvc\Model\Criteria
  */
 protected function newCriteria()
 {
     if (!$this->modelname) {
         throw new Exception('No valid model has been set');
     }
     $criteria = new Criteria();
     $criteria->setModelName($this->modelname)->setDI($this->getDI());
     return $criteria;
 }
Ejemplo n.º 3
0
 /**
  * Builds a \Phalcon\Mvc\Model\Criteria based on an input array like $_POST
  *
  * @param \Phalcon\DiInterface $dependencyInjector
  * @param string $modelName
  * @param array $data
  * @return \Phalcon\Mvc\Model\Criteria
  * @throws Exception
  */
 public static function fromInput($dependencyInjector, $modelName, $data)
 {
     if (is_object($dependencyInjector) === false || $dependencyInjector instanceof DiInterface === false) {
         throw new Exception('A dependency injector container is required to obtain the ORM services');
     }
     if (is_string($modelName) === false) {
         throw new Exception('Invalid parameter type.');
     }
     if (is_array($data) === false) {
         throw new Exception('Model data must be an Array');
     }
     if (empty($data) === false) {
         $conditions = array();
         $metaData = $dependencyInjector->getShared('modelsMetadata');
         $model = new $modelName();
         $dataTypes = $metaData->getDataTypes($model);
         $bind = array();
         //We look for attributes in the array passed as data
         foreach ($data as $field => $value) {
             if (isset($dataTypes[$field]) === true && is_null($value) === false && $value !== '') {
                 if ($dataTypes[$field] === 2) {
                     //For varchar types we use LIKE operator
                     $condition = $field . ' LIKE :' . $field . ':';
                     $bind[$field] = '%' . $value . '%';
                 } else {
                     //For the rest of data types we use a plain = operator
                     $condition = $field . '=:' . $field . ':';
                     $bind[$field] = $value;
                 }
                 $conditions[] = $condition;
             }
         }
     }
     //Create an object instance and pass the parameters to it
     $criteria = new Criteria();
     if (isset($conditions) === true && empty($conditions) === false) {
         $criteria->where(implode(' AND ', $conditions));
         $criteria->bind($bind);
     }
     $criteria->setModelName($modelName);
     return $criteria;
 }