Exemplo n.º 1
0
 /**
  * init the model
  */
 public function __construct()
 {
     $f3 = \Base::instance();
     $this->table = $f3->get('db_table_prefix') . $this->table;
     $this->db = 'DB';
     parent::__construct();
     // validation & error handler
     $class = get_called_class();
     // PHP 5.3 bug
     $saveHandler = function (\DB\Cortex $self) use($class) {
         $valid = true;
         foreach ($self->getFieldConfiguration() as $field => $conf) {
             if (isset($conf['type'])) {
                 $val = $self->get($field);
                 $model = strtolower(str_replace('\\', '.', $class));
                 // check required fields
                 if (isset($conf['required'])) {
                     $valid = \Validation::instance()->required($val, $field, 'error.' . $model . '.' . $field);
                 }
                 // check unique
                 if (isset($conf['unique'])) {
                     $valid = \Validation::instance()->unique($self, $val, $field, 'error.' . $model . '.' . $field);
                 }
                 if (!$valid) {
                     break;
                 }
             }
         }
         return $valid;
     };
     $this->beforesave($saveHandler);
 }
Exemplo n.º 2
0
 /**
  *  @see \DB\SQL\Mapper::__get($key)
  */
 public function &__get($key)
 {
     if ($key === "properties") {
         return $this->properties;
     }
     return parent::__get($key);
 }
Exemplo n.º 3
0
 /**
  * check if a model field value is unique
  * @param \DB\Cortex $model
  * @param mixed $val
  * @param string $field
  * @param string $context
  * @return bool
  */
 function unique($model, $val, $field, $context = null)
 {
     $valid = true;
     if (empty($val)) {
         return $valid;
     }
     if (!$this->f3->exists($context . '.unique', $errText)) {
         $errText = 'This ' . $field . ' is already taken';
     }
     $filter = $model->dry() ? array($field . ' = ?', $val) : array($field . ' = ? and _id != ?', $val, $model->_id);
     if ($model->findone($filter)) {
         $this->f3->error(400, $errText);
         \Flash::instance()->setKey($context, 'has-error');
         $valid = false;
     }
     return $valid;
 }
Exemplo n.º 4
0
 /**
  * add single model to collection
  * @param $model
  */
 function add(Cortex $model)
 {
     $model->addToCollection($this);
     $this->append($model);
 }
Exemplo n.º 5
0
 /**
  * Check whether a (multi)-column index exists or not on a table
  * related to this model
  * @param array $fields
  * @return bool|array
  */
 public static function indexExists(array $fields = array())
 {
     $tableModifier = self::getTableModifier();
     $df = parent::resolveConfiguration();
     $check = false;
     $indexKey = $df['table'] . '___' . implode('__', $fields);
     $indexList = $tableModifier->listIndex();
     if (array_key_exists($indexKey, $indexList)) {
         $check = $indexList[$indexKey];
     }
     return $check;
 }
Exemplo n.º 6
0
 public function __construct()
 {
     $db = \Base::instance()->get('SQLDB');
     parent::__construct($db, 'manufacturer');
 }
Exemplo n.º 7
0
 /**
  * @param string $key
  * @param mixed $val
  * @return mixed|void
  * @throws Exception\ValidationException
  */
 public function set($key, $val)
 {
     if ($key == 'active') {
         // prevent abuse
         return;
     }
     if ($key != 'updated') {
         if ($this->exists($key)) {
             $currentVal = $this->get($key);
             // if current value is not a relational object
             // and value has changed -> update table col
             if (!is_object($currentVal) && $currentVal != $val) {
                 $this->touch('updated');
             }
         }
     }
     // trim all values
     if (is_string($val)) {
         $val = trim($val);
     }
     $valid = $this->validateField($key, $val);
     if (!$valid) {
         $this->throwValidationError($key);
     } else {
         return parent::set($key, $val);
     }
 }
Exemplo n.º 8
0
 function run($db, $type)
 {
     $test = new \Test();
     $tname = 'test_cortex';
     \DB\Cortex::setdown($db, $tname);
     $fields = array('title' => array('type' => \DB\SQL\Schema::DT_TEXT), 'num1' => array('type' => \DB\SQL\Schema::DT_INT4), 'num2' => array('type' => \DB\SQL\Schema::DT_INT4));
     \DB\Cortex::setup($db, $tname, $fields);
     // adding some testing data
     $cx = new \DB\Cortex($db, $tname);
     $cx->title = 'bar1';
     $cx->save();
     $cx->reset();
     $cx->title = 'baz2';
     $cx->num1 = 1;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo3';
     $cx->num1 = 4;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo4';
     $cx->num1 = 3;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo5';
     $cx->num1 = 3;
     $cx->num2 = 5;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo6';
     $cx->num1 = 3;
     $cx->num2 = 1;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo7';
     $cx->num1 = 3;
     $cx->num2 = 10;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo8';
     $cx->num1 = 5;
     $cx->save();
     $cx->reset();
     $cx->title = 'foo9';
     $cx->num1 = 8;
     $cx->save();
     $cx->reset();
     $result = $this->getResult($cx->find());
     $expected = array(0 => array('title' => 'bar1'), 1 => array('num1' => 1, 'title' => 'baz2'), 2 => array('num1' => 4, 'title' => 'foo3'), 3 => array('num1' => 3, 'title' => 'foo4'), 4 => array('num1' => 3, 'num2' => 5, 'title' => 'foo5'), 5 => array('num1' => 3, 'num2' => 1, 'title' => 'foo6'), 6 => array('num1' => 3, 'num2' => 10, 'title' => 'foo7'), 7 => array('num1' => 5, 'title' => 'foo8'), 8 => array('num1' => 8, 'title' => 'foo9'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': init mapper, adding records');
     // operator =
     $result = $this->getResult($cx->find(array('title = ?', 'foo7')));
     $expected = array(0 => array('num1' => 3, 'num2' => 10, 'title' => 'foo7'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator check: =');
     // operator >
     $result = $this->getResult($cx->find(array('num1 > ?', 4)));
     $expected = array(0 => array('num1' => 5, 'title' => 'foo8'), 1 => array('num1' => 8, 'title' => 'foo9'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator check: >');
     // operator >=
     $result = $this->getResult($cx->find(array('num1 >= ?', 5)));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator check: >=');
     // operator <
     $result = $this->getResult($cx->find(array('num2 < ?', 2)));
     $expected = array(0 => array('num1' => 3, 'num2' => 1, 'title' => 'foo6'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator check: <');
     // operator <=
     $result = $this->getResult($cx->find(array('num2 <= ?', 1)));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator check: <=');
     // operator without binding
     $result = $this->getResult($cx->find(array('num1 > 4')));
     $expected = array(0 => array('num1' => 5, 'title' => 'foo8'), 1 => array('num1' => 8, 'title' => 'foo9'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': operator without binding');
     // field comparision
     $result = $this->getResult($cx->find(array('num2 > num1', 1)));
     $expected = array(0 => array('num1' => 3, 'num2' => 5, 'title' => 'foo5'), 1 => array('num1' => 3, 'num2' => 10, 'title' => 'foo7'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': check field comparision');
     // lookahead search
     $result = $this->getResult($cx->find(array('title like ?', '%o6')));
     $expected = array(0 => array('num1' => 3, 'num2' => 1, 'title' => 'foo6'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': lookahead search');
     // lookbehind search
     $result = $this->getResult($cx->find(array('title like ?', 'bar%')));
     $expected = array(0 => array('title' => 'bar1'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': lookbehind search');
     // full search
     $result = $this->getResult($cx->find(array('title like ?', '%a%')));
     $expected = array(0 => array('title' => 'bar1'), 1 => array('num1' => 1, 'title' => 'baz2'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': full search');
     // negated search
     $result = $this->getResult($cx->find(array('title not like ?', 'foo%')));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': negated search');
     // AND / OR chaining
     $result = $this->getResult($cx->find(array('(num2 < ? AND num1 > ?) OR title like ?', 2, 1, '%o9')));
     $expected = array(0 => array('num1' => 3, 'num2' => 1, 'title' => 'foo6'), 1 => array('num1' => 8, 'title' => 'foo9'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': check logical operator chaining');
     // check limit
     $result = $this->getResult($cx->find(null, array('limit' => '2')));
     $expected = array(0 => array('title' => 'bar1'), 1 => array('num1' => 1, 'title' => 'baz2'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': check limit');
     // check order
     $result = $this->getResult($cx->find(array('num2 >= ?', 1), array('order' => 'num2 desc')));
     $expected = array(0 => array('num1' => 3, 'num2' => 10, 'title' => 'foo7'), 1 => array('num1' => 3, 'num2' => 5, 'title' => 'foo5'), 2 => array('num1' => 3, 'num2' => 1, 'title' => 'foo6'));
     $test->expect(json_encode($result) == json_encode($expected), $type . ': check order');
     // IN search
     $rc = $cx->find(array('num1 IN ?', array(4, 5, 8)));
     $result = $rc->getAll('title');
     sort($result);
     $test->expect(json_encode($result) == json_encode(array('foo3', 'foo8', 'foo9')), $type . ': IN operator');
     $rc = $cx->find(array('num1 IN ? && num2 > ? && num2 NOT IN ?', array(3, 4), 1, array(10)));
     $result = $rc->getAll('title');
     $test->expect(json_encode($result) == json_encode(array('foo5')), $type . ': enhanced IN, NOT IN operator');
     ///////////////////////////////////
     return $test->results();
 }
Exemplo n.º 9
0
 public function __construct()
 {
     $this->fieldConf = array('author' => array('belongs-to-one' => '\\AuthorModel'), 'tags' => array('belongs-to-many' => '\\TagModel'), 'tags2' => array('has-many' => array('\\TagModel', 'news')));
     parent::__construct();
 }