Ejemplo n.º 1
0
 function save($o = null, $log = true)
 {
     $action = $this->id ? 'updated' : 'inserted';
     parent::save($o);
     if ($log) {
         $this->logActivity($action);
     }
 }
Ejemplo n.º 2
0
 function testShouldListPossibleParents()
 {
     $mapper = new DataMapper($this->db);
     $parent1_id = $mapper->save(array('name' => 'Parent 1'));
     $mapper = new DataMapper($this->db);
     $parent2_id = $mapper->save(array('name' => 'Parent 1a', 'paths' => array($parent1_id)));
     $form = new Form($mapper);
     $expected = array($parent1_id => 'Parent 1', $parent1_id . '/' . $parent2_id => 'Parent 1a');
     $this->assertEquals($expected, $form->getElement('paths')->getMultiOptions());
 }
Ejemplo n.º 3
0
 function testShouldUpdateAddress()
 {
     $address = array('first_name' => 'Joshua', 'last_name' => 'Ribakoff', 'email' => '*****@*****.**', 'address' => '123 Test St', 'address2' => 'Suite 5', 'city' => 'Port St Lucie', 'state' => 'FL', 'postal' => '00123', 'country' => 'USA', 'phone' => '0101010101', 'fax' => '0202020202');
     $addressMapper = new DataMapper($this->db);
     $id = $addressMapper->save($address);
     $updatedAddress = array('id' => $id, 'first_name' => 'Joshua-updated', 'last_name' => 'Ribakoff-updated', 'email' => '*****@*****.**', 'address' => '123 Test St-updated', 'address2' => 'Suite 5-updated', 'city' => 'Port St Lucie-updated', 'state' => 'FL-updated', 'postal' => '12345', 'country' => 'USA-updated', 'phone' => '111111111', 'fax' => '2222222222');
     $addressMapper = new DataMapper($this->db);
     $id = $addressMapper->save($updatedAddress);
     $loadedAddress = $addressMapper->load($id);
     $this->assertSame($updatedAddress, $loadedAddress, 'should save new address');
 }
Ejemplo n.º 4
0
 public function save($object = '', $related_field = '')
 {
     if ($this->_before_save() === false) {
         return false;
     }
     $result = parent::save($object, $related_field);
     if ($result) {
         $this->_after_save();
     }
     return $result;
 }
Ejemplo n.º 5
0
 function testShouldSaveCartAndItems()
 {
     $cart = new \Metator\Cart\Cart();
     $cart->add(1, 9.99);
     $cart->add(2, 4.99);
     $cart->setQuantity(2, 2);
     $order = array('items' => $cart, 'created' => '0000-00-00 00:00:00');
     $orderMapper = new DataMapper($this->db);
     $id = $orderMapper->save($order, null);
     $reloaded_order = $orderMapper->load($id);
     $this->assertEquals(array(1, 2), $reloaded_order['items']->items(), 'should save items');
 }
Ejemplo n.º 6
0
 function save($object = '')
 {
     if (empty($this->id) and empty($object)) {
         $s = new Section();
         $s->where('sort >=', $this->sort);
         //$s->where( 'parent_section', $this->parent_section );
         $s->get();
         foreach ($s->all as $item) {
             $item->sort++;
             $item->save();
         }
     }
     parent::save($object);
 }
Ejemplo n.º 7
0
 /**
  * save current section 
  * and prevent collision if it is a new object
  */
 public function save($object = '', $related_field = '')
 {
     if (empty($this->id) and empty($object)) {
         $s = new Section();
         $s->where('sort >=', $this->sort);
         $s->where('parent_section', $this->parent_section);
         $s->get();
         foreach ($s as $item) {
             $item->sort++;
             $item->save();
         }
     }
     parent::save($object, $related_field);
 }
Ejemplo n.º 8
0
 function save($object = '', $related_field = '')
 {
     if (!$this->exists() || empty($this->code)) {
         $o = new CurriculumViate();
         $o->select_max('position');
         $o->get();
         if (count($o->all) != 0) {
             $max = $o->position + 1;
             $this->position = $max;
         } else {
             $this->postion = 1;
         }
     }
     return parent::save($object, $related_field);
 }
Ejemplo n.º 9
0
 function save($object = '', $related_field = '')
 {
     if (!$this->exists()) {
         $o = new Estatetype();
         $o->select_max('position');
         $o->get();
         if (count($o->all) != 0) {
             $max = $o->position + 1;
             $this->position = $max;
         } else {
             $this->postion = 1;
         }
     }
     return parent::save($object, $related_field);
 }
Ejemplo n.º 10
0
 function save($object = '', $related_field = '')
 {
     if (!$this->exists()) {
         $o = new productcatspec();
         $o->where("productcat_id", $this->productcat_id);
         $o->select_max('position');
         $o->get();
         if (count($o->all) != 0) {
             $max = $o->position + 1;
             $this->position = $max;
         } else {
             $this->postion = 1;
         }
     }
     return parent::save($object, $related_field);
 }
Ejemplo n.º 11
0
 public function save($task = null, $user = null)
 {
     if ($user == null) {
         $user = $this->user;
     }
     if ($task == null) {
         $task = $this->task;
     }
     if ($user == null) {
         throw new UserNotFound();
     }
     if ($task == null) {
         throw new Task_Not_Found();
     }
     $this->checkOverlap($task);
     return parent::save(array($user, $task));
 }
Ejemplo n.º 12
0
 function save($object = '', $related_field = '')
 {
     if (!$this->exists() || empty($this->code)) {
         $o = new Cartitem();
         $o->select_max('position');
         $o->get();
         if (count($o->all) != 0) {
             $max = $o->position + 1;
             $this->position = $max;
         } else {
             $this->postion = 1;
         }
         $position = (string) $this->position;
         $l = 6 - strlen($position);
         $st = "";
         for ($i = 0; $i < $l; $i++) {
             $st .= "0";
         }
         $position = $st . $position;
         $this->code = $position;
     }
     return parent::save($object, $related_field);
 }
Ejemplo n.º 13
0
 /**
  * @param string $object
  * @param string $related_field
  * @return bool
  */
 public function save($object = '', $related_field = '')
 {
     $this->last_check = time();
     $result = parent::save($object, $related_field);
     return $result;
 }
Ejemplo n.º 14
0
 public function save($object = '', $related_field = '')
 {
     $is_new = FALSE;
     if ($this->_force_save_as_new or !$this->id) {
         $is_new = TRUE;
     }
     $this->trigger_event('before_save');
     /* keep copy of the stored because it resets to new after save */
     $this->_keep_old();
     $return = parent::save($object, $related_field);
     /* if new then get it to load relations */
     if ($return && $is_new && ($this->has_one or $this->has_many)) {
         $this->where('id', $this->id)->get();
     }
     if ($return) {
         $this->trigger_event('after_save');
     }
     return $return;
 }
Ejemplo n.º 15
0
 /**
  * Convert an associative array back into a DataMapper model.
  *
  * If $fields is provided, missing fields are assumed to be empty checkboxes.
  * Alse if $sub_array is TRUE, will save related object provided by sub array
  *
  * @param    DataMapper $object The DataMapper Object to save to.
  * @param    array $data A an associative array of fields to convert.
  * @param    array $fields Array of 'safe' fields.  If empty, only includes the database columns.
  * @param    bool $sub_array If TRUE - will save provided by subarray related obj
  * @param    bool $save If TRUE, then attempt to save the object automatically.
  * @return    array|bool A list of newly related objects, or the result of the save if $save is TRUE.
  * If $sub_array TRUE, return array('error','created_ids')
  */
 function save_from($object, $data, $fields = '', $sub_array = FALSE, $save = TRUE)
 {
     // keep track of newly related objects
     $new_related_objects = array();
     $error = '';
     //error string
     $created_ids = array();
     //stores array of newly created ids ('related_class_name' => array(ids))
     // Assume all database columns.
     // In this case, simply store $fields that are in the $data array.
     $object_fields = $object->fields;
     if (in_array('password', $object_fields)) {
         $object_fields[] = 'password_confirm';
     }
     foreach ($data as $k => $v) {
         if (in_array($k, $object_fields)) {
             $object->{$k} = $v;
         }
     }
     if (!empty($fields)) {
         // If $fields is provided, assume all $fields should exist.
         foreach ($fields as $f) {
             if (array_key_exists($f, $object->has_one)) {
                 // Store $has_one relationships
                 $c = get_class($object->{$f});
                 $rel = new $c();
                 $id = isset($data[$f]) ? $data[$f] : 0;
                 $rel->get_by_id($id);
                 if ($rel->exists()) {
                     // The new relationship exists, save it.
                     $new_related_objects[$f] = $rel;
                 } else {
                     // The new relationship does not exist, delete the old one.
                     $object->delete($object->{$f}->get());
                 }
             } else {
                 if (array_key_exists($f, $object->has_many)) {
                     // Store $has_many relationships
                     $c = get_class($object->{$f});
                     $ids = isset($data[$f]) ? $data[$f] : FALSE;
                     $has_join_table = false;
                     if (!empty($object->has_many[$f]['join_table'])) {
                         $has_join_table = true;
                         //so, do not delete the record, only the relation
                     }
                     if ($sub_array) {
                         $error_classes_to_check = array();
                         $created_ids[$f] = array();
                         if (empty($ids)) {
                             $old_related_obj = $object->{$f}->select('id')->get();
                             if ($has_join_table) {
                                 $object->delete($old_related_obj);
                                 //delete relation to related object
                             } else {
                                 $old_related_obj->delete_all();
                                 //delete related object
                             }
                         } else {
                             $related_ids = array();
                             foreach ($ids as $related_row) {
                                 $create = false;
                                 if (isset($related_row['id']) && $related_row['id']) {
                                     $id = $related_row['id'];
                                     $related_ids[] = $id;
                                     //add to list of provided ids
                                     unset($related_row['id']);
                                     //useless any more
                                 } else {
                                     $id = null;
                                     $create = true;
                                 }
                                 $related_obj = new $c($id);
                                 foreach ($related_row as $column => $value) {
                                     $related_obj->{$column} = $value;
                                     //fill object with new values
                                 }
                                 if ($create) {
                                     //create new record
                                     if (!$related_obj->save()) {
                                         $error .= $related_obj->error->string;
                                     } else {
                                         $created_ids[$f][] = $related_obj->id;
                                         //add id to returned array
                                     }
                                 }
                                 $error_classes_to_check[] = $f;
                                 $new_related_objects[$f][] = $related_obj;
                             }
                             //delete not provided objects
                             if (!empty($related_ids)) {
                                 $old_related_obj = $object->{$f}->where_not_in('id', $related_ids)->select('id')->get();
                             } else {
                                 $old_related_obj = $object->{$f}->select('id')->get();
                             }
                             if ($has_join_table) {
                                 $object->delete($old_related_obj);
                             } else {
                                 $old_related_obj->delete_all();
                             }
                         }
                     } else {
                         if (empty($ids)) {
                             $object->delete(array($f => $object->{$f}->select('id')->get()->all));
                         } else {
                             $rels = new $c();
                             // Otherwise, get the new ones...
                             $rels->where_in('id', $ids)->select('id')->get();
                             // Store them...
                             $new_related_objects[$f] = $rels->all;
                             // And delete any old ones that do not exist.
                             $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
                             $object->delete(array($f => $old_rels->all));
                         }
                     }
                 } else {
                     // Otherwise, if the $data was set, store it...
                     if (isset($data[$f])) {
                         $v = $data[$f];
                     } else {
                         // Or assume it was an unchecked checkbox, and clear it.
                         $v = FALSE;
                     }
                     $object->{$f} = $v;
                 }
             }
         }
     }
     if ($save) {
         if ($sub_array) {
             $object->save_from = new stdClass();
             //add properties to object
             $object->save_from->error = $error;
             $object->save_from->created_ids = $created_ids;
             if (empty($error)) {
                 //if no error while creating new related objects
                 $object->save($new_related_objects);
                 $error .= $object->error->string;
                 foreach ($error_classes_to_check as $error_class) {
                     $error .= $object->{$error_class}->error->string;
                     //add errors from updated related objects
                 }
                 $object->save_from->error = $error;
                 if (empty($error)) {
                     return true;
                 } else {
                     //remove created objects, because some of relation couldn't be saved
                     if (isset($created_ids) && !empty($created_ids) && is_array($created_ids)) {
                         foreach ($created_ids as $related_class_name => $related_ids) {
                             $class = get_class($object->{$related_class_name});
                             foreach ($related_ids as $created_id) {
                                 $related_class = new $class($created_id);
                                 $related_class->delete();
                             }
                         }
                     }
                     return false;
                 }
             } else {
                 //remove created objects, because some of relation couldn't be saved
                 if (isset($created_ids) && !empty($created_ids) && is_array($created_ids)) {
                     foreach ($created_ids as $related_class_name => $related_ids) {
                         $class = get_class($object->{$related_class_name});
                         foreach ($related_ids as $created_id) {
                             $related_class = new $class($created_id);
                             $related_class->delete();
                         }
                     }
                 }
                 return false;
             }
         }
         return $object->save($new_related_objects);
     } else {
         // return new objects
         return $new_related_objects;
     }
 }
Ejemplo n.º 16
0
 /**
  * Convert an associative array back into a DataMapper model.
  *
  * If $fields is provided, missing fields are assumed to be empty checkboxes.
  *
  * @param	DataMapper $object The DataMapper Object to save to.
  * @param	array $data A an associative array of fields to convert.
  * @param	array $fields Array of 'safe' fields.  If empty, only includes the database columns.
  * @param	bool $save If TRUE, then attempt to save the object automatically.
  * @return	array|bool A list of newly related objects, or the result of the save if $save is TRUE
  */
 function from_array($object, $data, $fields = '', $save = FALSE)
 {
     // keep track of newly related objects
     $new_related_objects = array();
     // Assume all database columns.
     // In this case, simply store $fields that are in the $data array.
     if (empty($fields)) {
         $fields = $object->fields;
         foreach ($data as $k => $v) {
             if (in_array($k, $fields)) {
                 $object->{$k} = $v;
             }
         }
     } else {
         // If $fields is provided, assume all $fields should exist.
         foreach ($fields as $f) {
             if (array_key_exists($f, $object->has_one)) {
                 // Store $has_one relationships
                 $c = get_class($object->{$f});
                 $rel = new $c();
                 $id = isset($data[$f]) ? $data[$f] : 0;
                 $rel->get_by_id($id);
                 if ($rel->exists()) {
                     // The new relationship exists, save it.
                     $new_related_objects[$f] = $rel;
                 } else {
                     // The new relationship does not exist, delete the old one.
                     $object->delete($object->{$f}->get());
                 }
             } else {
                 if (array_key_exists($f, $object->has_many)) {
                     // Store $has_many relationships
                     $c = get_class($object->{$f});
                     $rels = new $c();
                     $ids = isset($data[$f]) ? $data[$f] : FALSE;
                     if (empty($ids)) {
                         // if no IDs were provided, delete all old relationships.
                         $object->delete($object->{$f}->select('id')->get()->all);
                     } else {
                         // Otherwise, get the new ones...
                         $rels->where_in('id', $ids)->select('id')->get();
                         // Store them...
                         $new_related_objects[$f] = $rels->all;
                         // And delete any old ones that do not exist.
                         $old_rels = $object->{$f}->where_not_in('id', $ids)->select('id')->get();
                         $object->delete($old_rels->all);
                     }
                 } else {
                     // Otherwise, if the $data was set, store it...
                     if (isset($data[$f])) {
                         $v = $data[$f];
                     } else {
                         // Or assume it was an unchecked checkbox, and clear it.
                         $v = FALSE;
                     }
                     $object->{$f} = $v;
                 }
             }
         }
     }
     if ($save) {
         // Auto save
         return $object->save($new_related_objects);
     } else {
         // return new objects
         return $new_related_objects;
     }
 }
 /**
  * @param string $object
  * @param string $related_field
  * @return bool
  */
 public function save($object = '', $related_field = '')
 {
     $result = parent::save($object, $related_field);
     return $result;
 }
Ejemplo n.º 18
0
 /**
  * @throws UsernameAlreadyExistException
  * @throws EmailAlreadyExistException
  */
 public function save($object = '', $related_field = '')
 {
     return parent::save($object, $related_field);
 }
Ejemplo n.º 19
0
 function testShouldCountByCategoryAndAttribute()
 {
     $product_mapper = new DataMapper($this->db);
     // should find
     $product_mapper->save(new Product(array('sku' => 'foo1', 'categories' => [1], 'attributes' => ['color' => 'red'])));
     // should not find
     $product_mapper->save(new Product(array('sku' => 'foo2', 'categories' => [2], 'attributes' => ['color' => 'red'])));
     // should not find
     $product_mapper->save(new Product(array('sku' => 'foo3', 'categories' => [1], 'attributes' => ['color' => 'blue'])));
     $count = $product_mapper->count(array('category' => 1, 'attributes' => ['color' => 'red']));
     $this->assertEquals(1, $count, 'should count by attribute & category');
 }
Ejemplo n.º 20
0
 public function save($object = '', $related_field = '')
 {
     $is_update = isset($this->id) ? TRUE : FALSE;
     if (!empty($this->changed_related_values)) {
         // recursive terminate
         $orginal_related_values = array_merge(array(), $this->orginal_related_values);
         $changed_related_values = array_merge(array(), $this->changed_related_values);
         $this->orginal_related_values = array();
         $this->changed_related_values = array();
         $this->_auto_trans_start();
         $delete_related_fields = array();
         $save_related_fields = array();
         foreach ($changed_related_values as $field => $changed) {
             foreach ($changed as $action => $o_list) {
                 foreach ($o_list as $o) {
                     if ($o->force_save) {
                         if (!$o->save()) {
                             $this->error_message($field, $o->error->string);
                             $this->_auto_trans_terminate();
                             return FALSE;
                         }
                     }
                     if ($action == 'save') {
                         $dest_related_fields =& $save_related_fields;
                     } else {
                         if ($action == 'delete') {
                             $dest_related_fields =& $delete_related_fields;
                         } else {
                             continue;
                         }
                     }
                     if (isset($this->has_one[$field])) {
                         $dest_related_fields[$field] = $o;
                     } else {
                         if (!isset($dest_related_fields[$field])) {
                             $dest_related_fields[$field] = array();
                         }
                         $dest_related_fields[$field][] = $o;
                     }
                 }
             }
         }
         if (!empty($delete_related_fields)) {
             if (!parent::delete($delete_related_fields)) {
                 $this->error_message('_delete', $object->error->string);
                 $this->_auto_trans_terminate();
                 return FALSE;
             }
         }
         if (is_array($object)) {
             $save_related_fields = $object + $save_related_fields;
         } else {
             if (!empty($related_field)) {
                 $save_related_fields[$related_field] = $object;
             }
         }
         if (parent::save($save_related_fields)) {
             if ($this->_auto_trans_complete('MDI_Model save changed related fields')) {
                 $this->transaction_history = array();
             } else {
                 $this->orginal_related_values = array_merge(array(), $orginal_related_values);
                 $this->changed_related_values = array_merge(array(), $changed_related_values);
                 return FALSE;
             }
         } else {
             $this->orginal_related_values = array_merge(array(), $orginal_related_values);
             $this->changed_related_values = array_merge(array(), $changed_related_values);
             $this->_auto_trans_terminate();
             return FALSE;
         }
     } else {
         if (!parent::save($object, $related_field)) {
             return FALSE;
         }
     }
     // history
     if (mdi::config('admin_using_history')) {
         if ($is_update) {
             $this->_save_history($this->id, 'modify');
         } else {
             $this->_save_history($this->id, 'new');
         }
     }
     return TRUE;
 }
Ejemplo n.º 21
0
 public function update()
 {
     return parent::save();
 }
Ejemplo n.º 22
0
 public function save($user = null)
 {
     if ($user == null && $this->user == null) {
         throw new UserNotFound();
     }
     if ($user == null) {
         $user = $this->user;
     }
     $this->checkExist($user, $this->name);
     parent::save($user);
 }
Ejemplo n.º 23
0
 /**
  * save current content ot the system
  * it attach the content automatically
  * 
  * @param unknown_type $object
  * @param unknown_type $related_field
  * @return boolean
  */
 public function save($object = '', $related_field = '')
 {
     if (empty($this->id) and empty($object)) {
         $this->attach();
     }
     return parent::save($object, $related_field);
 }
Ejemplo n.º 24
0
 function testShouldFindStructured()
 {
     $mapper = new DataMapper($this->db);
     $foo_id = $mapper->save(array('name' => 'foo'));
     $bar_id = $mapper->save(array('name' => 'bar', 'paths' => array($foo_id)));
     $baz_id = $mapper->save(array('name' => 'baz', 'paths' => array($foo_id . '/' . $bar_id)));
     $categories = $mapper->findStructuredAll();
     $expected = array(array('id' => $foo_id, 'name' => 'foo', 'paths' => array(), 'active' => 1, 'children' => array(array('id' => $bar_id, 'name' => 'bar', 'paths' => array($foo_id), 'active' => 1, 'children' => array(array('id' => $baz_id, 'name' => 'baz', 'paths' => array($foo_id . '/' . $bar_id), 'active' => 1, 'children' => array()))))));
     $this->assertEquals($expected, $categories);
 }
Ejemplo n.º 25
0
 public function save($object = '', $related_field = '')
 {
     $this->trigger_event('before_save', $this);
     /* keep copy of the stored because it resets to new after save */
     $this->_keep_old();
     $return = parent::save($object, $related_field);
     if ($return) {
         $this->trigger_event('after_save', $this);
     }
     return $return;
 }