Esempio n. 1
0
 /**
  * Provider for test_add_remove
  */
 public function provider_add_remove()
 {
     // For has many
     $author = Jelly::factory('test_author', 1);
     // For many to many
     $post = Jelly::factory('test_post', 1);
     return array(array($author, 'test_posts', 1, 1), array($author, 'test_posts', Jelly::query('test_post')->select(), 2), array($author, 'test_posts', array(1, 2), 2), array($author, 'test_posts', 999, 0), array($author, 'test_posts', array(999, 998, 997, 45), 0), array($author, 'test_posts', array(Jelly::factory('test_post')), 0), array($post, 'test_categories', 1, 1), array($post, 'test_categories', Jelly::query('test_category')->select(), 3), array($post, 'test_categories', array(1, 2), 2), array($post, 'test_categories', 999, 0), array($post, 'test_categories', array(999, 998, 997, 45), 0), array($post, 'test_categories', array(Jelly::factory('test_category')), 0));
 }
Esempio n. 2
0
 public function __construct($field, $smarty)
 {
     parent::__construct($field, $smarty);
     //first empty element
     $this->_options[0] = __('select');
     $options = Jelly::query($this->_field->foreign['model'])->select()->as_array('id', $this->_field->form_field);
     //push options
     array_walk($options, function (&$item, $key) {
         $this->_options[$key] = $item;
     });
 }
Esempio n. 3
0
 /**
  * Returns the Jelly model that this model belongs to.
  *
  * @param   Jelly_Model    $model
  * @param   mixed          $value
  * @return  Jelly_Builder
  */
 public function get($model, $value)
 {
     return Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . $this->foreign['field'], '=', $value)->limit(1);
 }
Esempio n. 4
0
 /**
  * Get the stored password for a username.
  *
  * @param   mixed   username string, or user Jelly object
  * @return  string
  */
 public function password($user)
 {
     if (!is_object($user)) {
         $username = $user;
         // Load the user
         $user = Jelly::query('User')->where(Jelly::factory('User')->unique_key($username), '=', $username)->limit(1)->select();
     }
     return $user->password;
 }
Esempio n. 5
0
 public function __construct($field, $smarty)
 {
     parent::__construct($field, $smarty);
     $tags = Jelly::query($this->_field->model)->select();
 }
Esempio n. 6
0
 /**
  * Loads a role based on name.
  *
  * @param	string	$role
  * @return	Jelly_Model
  */
 public function get_role($role)
 {
     return Jelly::query('role')->where('name', '=', $role)->limit(1)->select();
 }
Esempio n. 7
0
 /**
  * Returns either an array or unexecuted query to find
  * which columns the model is "in" in the join table.
  *
  * @param   Jelly_Model  $model
  * @param   boolean      $as_array
  * @return  mixed
  */
 protected function _in($model, $as_array = FALSE)
 {
     $result = Jelly::query($this->through['model'])->select_column($this->through['fields'][1], 'in')->where($this->through['fields'][0], '=', $model->id())->type(Database::SELECT);
     if ($as_array) {
         $result = $result->select($model->meta()->db())->as_array(NULL, 'in');
     }
     return $result;
 }
Esempio n. 8
0
 /**
  * Easy-to-override method that expands aliases.
  *
  * @param   string  $model
  * @param   string  $alias
  * @param   array   $state
  * @return  array
  */
 protected function _expand_alias($model, $alias, $state)
 {
     switch ($alias) {
         case ':primary_key':
             $state['field'] = Jelly::meta($model)->primary_key();
             break;
         case ':name_key':
             $state['field'] = Jelly::meta($model)->name_key();
             break;
         case ':foreign_key':
             $state['field'] = Jelly::meta($model)->foreign_key();
             break;
         case ':unique_key':
             $state['field'] = Jelly::query(Jelly::meta($model)->model())->unique_key($state['value']);
             break;
         default:
             throw new Kohana_Exception('Unknown meta alias :alias', array(':alias' => $alias));
     }
     return $state;
 }
Esempio n. 9
0
 /**
  * Implementation of Jelly_Field_Supports_Save.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $value
  * @param   boolean      $loaded
  * @return  void
  */
 public function save($model, $value, $loaded)
 {
     // Don't do anything on INSERTs when there is nothing in the value
     if (!$loaded and empty($value)) {
         return;
     }
     // Empty relations to the default value
     Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . $this->foreign['field'], '=', $model->id())->set(array($this->foreign['field'] => $this->foreign_default))->update();
     // Set the new relations
     if (!empty($value)) {
         // Update the ones in our list
         Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . ':primary_key', '=', $value)->set(array($this->foreign['field'] => $model->id()))->update();
     }
 }
Esempio n. 10
0
 /**
  * Find unique slug
  * 
  * @param string $slug converted slug
  * @return string unique slug
  */
 protected final function _unique($slug)
 {
     //find all occurrences
     $items = Jelly::query($this->model)->where($this->name, 'LIKE', $slug . '%')->count();
     if ((bool) $items) {
         return $slug . '-' . ($items + 1);
     }
     return $slug;
 }
Esempio n. 11
0
 /**
  * Callback for validating that a field is unique.
  *
  * @param   Validation   $data
  * @param   Jelly_Model  $model
  * @param   string       $field
  * @param   string       $value
  * @return  void
  */
 public function _is_unique(Validation $data, Jelly_Model $model, $field, $value)
 {
     // According to the SQL standard NULL is not checked by the unique constraint
     // We also skip this test if the value is the same as the default value
     if ($value !== NULL and $value !== $this->default) {
         // Build query
         $query = Jelly::query($model)->where($field, '=', $value);
         // Limit to one
         $query->limit(1);
         if ($query->count()) {
             // Add error if duplicate found
             $data->error($field, 'unique');
         }
     }
 }
Esempio n. 12
0
 /**
  * Implementation of Jelly_Field_Supports_Has.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $models
  * @return  boolean
  */
 public function has($model, $models)
 {
     return (bool) Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . $this->foreign['field'], '=', $model->id())->where($this->foreign['model'] . '.' . ':primary_key', 'IN', $this->_ids($models))->count();
 }
Esempio n. 13
0
 /**
  * Deletes a single record.
  *
  * @return  boolean
  **/
 public function delete()
 {
     $result = FALSE;
     // Are we loaded? Then we're just deleting this record
     if ($this->_loaded) {
         $key = $this->_original[$this->_meta->primary_key()];
         // Trigger callbacks to ensure we proceed
         $result = $this->_meta->events()->trigger('model.before_delete', $this);
         if ($result === NULL) {
             // Trigger field callbacks
             foreach ($this->_meta->fields() as $field) {
                 $field->delete($this, $key);
             }
             $result = Jelly::query($this, $key)->delete();
         }
     }
     // Trigger the after-delete
     $this->_meta->events()->trigger('model.after_delete', $this);
     // Clear the object so it appears deleted anyway
     $this->clear();
     return (bool) $result;
 }
Esempio n. 14
0
 /**
  * Test for Issue #95. This only fails when testing on Postgres.
  *
  * @return  void
  */
 public function test_count_works_on_postgres()
 {
     // Should discard the select and order_by clauses
     Jelly::query('test_post')->select_column('foo')->order_by('foo')->count();
 }