Example #1
0
 public function model_before_check(Jam_Model $model)
 {
     foreach ($this->_parents as $child => $parent) {
         $child_association = $model->meta()->association_insist($child);
         $parent_association = $model->meta()->association_insist($parent);
         if ($model->changed($child) and $model->{$child} and !$model->{$child}->parent and $model->{$parent}) {
             $model->{$child}->parent = $model->{$parent};
         }
     }
 }
Example #2
0
 public function model_call_update_token(Jam_Model $model, Jam_Event_Data $data)
 {
     do {
         $model->{$this->_field} = $this->new_token();
     } while (Jam::all($model->meta()->model())->where($this->_field, '=', $model->{$this->_field})->count_all() > 0);
     $data->return = $model;
 }
Example #3
0
 public function collection(Jam_Model $model)
 {
     $collection = Jam::all($this->foreign_model);
     $collection->join_table($this->join_table)->context_model($this->foreign_model)->on($this->join_table . '.' . $this->term_key, '=', ':primary_key')->end()->where($this->join_table . '.' . $this->item_polymorphic_key, '=', DB::expr(':model', array(':model' => $model->meta()->model())))->where($this->join_table . '.' . $this->item_key, '=', $model->id());
     if ($this->vocabulary_ids()) {
         $collection->where($this->vocabulary_foreign_key, 'IN', $this->vocabulary_ids());
     }
     return $collection;
 }
 /**
  * Call the given method an all the child associations.
  *
  * @param  Jam_Model $model
  * @param  string    $method_name
  */
 public function call_associations_method(Jam_Model $model, $method_name)
 {
     foreach ($this->_associations as $name) {
         if ($model->meta()->association($name) instanceof Jam_Association_Collection) {
             foreach ($model->{$name}->as_array() as $item) {
                 $item->{$method_name}();
             }
         } elseif ($model->{$name}) {
             $model->{$name}->{$method_name}();
         }
         $model->{$name} = $model->{$name};
     }
 }
 /**
  * Generate a query to add models from the association (without deleting them), for specific ids
  * @param  Jam_Model $model
  * @param  array     $ids  
  * @return Database_Query
  */
 public function add_items_query(Jam_Model $model, array $ids)
 {
     $query = DB::insert($this->branches_table)->columns(array($this->ansestor_key, $this->descendant_key, $this->depth_key));
     foreach ($ids as $id) {
         foreach ($this->ansestors_query($model->id())->execute($model->meta()->db()) as $ansestor) {
             $query->values(array($ansestor[$this->ansestor_key], $id, $ansestor[$this->depth_key] + 1));
             foreach ($this->descendants_query($id)->where($this->depth_key, '>', 0)->execute($model->meta()->db()) as $descendant) {
                 $query->values(array($ansestor[$this->ansestor_key], $descendant[$this->descendant_key], $ansestor[$this->depth_key] + $descendant[$this->depth_key] + 1));
             }
         }
     }
     return $query;
 }
 public function model_after_delete(Jam_Model $model)
 {
     DB::delete($this->_branches_table)->where($this->_ansestor_key, '=', $model->id())->where($this->_descendant_key, '=', $model->id())->execute($model->meta()->db());
 }
Example #7
0
 public function execute(Jam_Model $model)
 {
     foreach ($this->callbacks() as $method => $model_names) {
         $children = Jam_Behavior_Cascade::get_current_children($model->meta()->model(), $model_names);
         $models = Jam_Behavior_Cascade::collect_models($model, (array) $children);
         call_user_func($method, $model, $models);
     }
 }
Example #8
0
 public function query_builder($type, Jam_Model $model)
 {
     $query = call_user_func("Jam::{$type}", $this->foreign_model)->where($this->foreign_key, '=', $model->id());
     if ($this->is_polymorphic()) {
         $query->where($this->polymorphic_key, '=', $model->meta()->model());
     }
     return $query;
 }
Example #9
0
 private function _model_messages_dump(Jam_Model $model)
 {
     $messages = array();
     foreach ($model->errors() as $attribute_name => $errors) {
         if ($model->meta()->association($attribute_name) instanceof Jam_Association_Collection) {
             foreach ($model->{$attribute_name} as $i => $item) {
                 if (!$item->is_valid()) {
                     $messages[] = UTF8::ucfirst(Inflector::humanize($attribute_name)) . ' (' . $i . '): ' . join(', ', $this->_model_messages_dump($item));
                 }
             }
         } elseif ($model->meta()->association($attribute_name) and $model->{$attribute_name}) {
             $messages[] = UTF8::ucfirst(Inflector::humanize($attribute_name)) . ': ' . join(', ', $this->_model_messages_dump($model->{$attribute_name}));
         } else {
             foreach ($errors as $error => $params) {
                 $messages[] = Jam_Errors::message($model->meta()->errors_filename(), $attribute_name, $error, Arr::merge($params, array(':model' => $model->meta()->model(), ':attribute' => Jam_Errors::attribute_label($model->meta(), $attribute_name))));
             }
         }
     }
     return $messages;
 }
Example #10
0
 public function set_query(Jam_Model $model, $ansestor_id)
 {
     $query = DB::insert($this->branches_table)->columns(array($this->ansestor_key, $this->descendant_key, $this->depth_key));
     foreach ($this->ansestors_query($ansestor_id)->execute($model->meta()->db()) as $ansestor) {
         $query->values(array($ansestor[$this->ansestor_key], $model->id(), $ansestor[$this->depth_key] + 1));
     }
     return $query;
 }