Example #1
0
 /**
  * Save the object to the database. If $verify is set, it will
  * validate the data against any rules in the array, or in the
  * specified INI file if $verify is a string matching a file name.
  */
 public function put()
 {
     $f = new Form();
     $failed = $f->verify_values($this->data, $this->verify);
     if (!empty($failed)) {
         $this->failed = $failed;
         $this->error = 'Validation failed for: ' . join(', ', $failed);
         return false;
     } else {
         $this->failed = array();
     }
     if ($this->is_new) {
         // This is an insert
         $ins = array();
         $len = count($this->data);
         for ($i = 0; $i < $len; $i++) {
             $ins[] = '?';
         }
         if (!$this->collection->insert($this->data)) {
             $err = $this->db->lastError();
             $this->error = $err['err'];
             return false;
         }
         $this->keyval = $this->data[$this->key];
         $this->is_new = false;
         return true;
     }
     // This is an update
     if (!$this->keyval) {
         $this->keyval = $this->data[$this->key];
     }
     if (!$this->collection->update(array('_id' => $this->_id($this->keyval)), $this->data)) {
         $err = $this->db->lastError();
         $this->error = $err['err'];
         return false;
     }
     $this->is_new = false;
     return true;
 }
Example #2
0
 function test_verify_values()
 {
     $values = array('foo' => 'bar', 'asdf' => 'qwerty');
     $validations = array('foo' => array('not empty' => 1), 'asdf' => array('empty' => 1));
     $f = new Form();
     $this->assertEquals(array('asdf'), $f->verify_values($values, $validations));
     $validations = array('foo' => array('skip_if_empty' => 1, 'contains' => 'asdf'));
     $values = array('foo' => '', 'asdf' => 'qwerty');
     $this->assertEquals(array(), $f->verify_values($values, $validations));
     $values['foo'] = 'foobar';
     $this->assertEquals(array('foo'), $f->verify_values($values, $validations));
     $values['foo'] = 'asdf';
     $this->assertEquals(array(), $f->verify_values($values, $validations));
     $validations = array('foo' => array('type' => 'array', 'skip_if_empty' => 1, 'each contains' => 'asdf'));
     $values = array('foo' => 'asdf');
     // Not an array
     $this->assertEquals(array('foo'), $f->verify_values($values, $validations));
     $values = array('foo' => array('bar', ''));
     // Contains should fail
     $this->assertEquals(array('foo'), $f->verify_values($values, $validations));
     $values = array('foo' => array('', ''));
     // All empty should pass
     $this->assertEquals(array(), $f->verify_values($values, $validations));
 }
Example #3
0
 /**
  * Need to verify extended fields, so we override the put() method.
  */
 public function put()
 {
     $f = new Form();
     $failed = $f->verify_values($this->ext(), $this->_extended_verify);
     if (!empty($failed)) {
         $this->error = 'Validation failed for extended fields: ' . join(', ', $failed);
         return false;
     }
     return parent::put();
 }
Example #4
0
 /**
  * Save the object to the database. If $verify is set, it will
  * validate the data against any rules in the array, or in the
  * specified INI file if $verify is a string matching a file name.
  */
 public function put()
 {
     $f = new Form();
     $failed = $f->verify_values($this->data, $this->verify);
     if (!empty($failed)) {
         $this->failed = $failed;
         $this->error = 'Validation failed for: ' . join(', ', $failed);
         return false;
     } else {
         $this->failed = array();
     }
     if ($this->is_new) {
         // This is an insert
         $ins = array();
         $len = count($this->data);
         for ($i = 0; $i < $len; $i++) {
             $ins[] = '?';
         }
         if (!DB::execute('insert into `' . $this->table . '` (' . join(', ', Model::backticks(array_keys($this->data))) . ') values (' . join(', ', $ins) . ')', $this->data)) {
             $this->error = DB::error();
             return false;
         }
         if (!isset($this->data[$this->key])) {
             $this->data[$this->key] = DB::last_id();
             $this->keyval = $this->data[$this->key];
         }
         $this->is_new = false;
         return true;
     }
     // This is an update
     $ins = '';
     $par = array();
     $sep = '';
     foreach ($this->data as $key => $val) {
         $ins .= $sep . Model::backticks($key) . ' = ?';
         $par[] = $val;
         $sep = ', ';
     }
     if ($this->keyval && $this->keyval !== $this->data[$this->key]) {
         $par[] = $this->keyval;
     } else {
         $par[] = $this->data[$this->key];
         $this->keyval = $this->data[$this->key];
     }
     if (!DB::execute('update `' . $this->table . '` set ' . $ins . ' where `' . $this->key . '` = ?', $par)) {
         $this->error = DB::error();
         return false;
     }
     $this->is_new = false;
     return true;
 }