/** * @covers Jam::insert */ public function test_insert() { $insert = Jam::insert('test_author'); $this->assertInstanceOf('Jam_Query_Builder_Insert', $insert); $this->assertSame(Jam::meta('test_author'), $insert->meta()); $this->assertEquals('INSERT INTO `test_authors` () VALUES ', $insert->compile()); $insert = Jam::insert('test_author', array('name', 'email')); $this->assertEquals('INSERT INTO `test_authors` (`name`, `email`) VALUES ', $insert->compile()); }
/** * Creates or updates the current record. * * @param bool|null $validate * @return Kohana_Jam_Model */ public function save($validate = NULL) { if ($this->_is_saving) { throw new Kohana_Exception("Cannot save a model that is already in the process of saving"); } $key = $this->_original[$this->meta()->primary_key()]; // Run validation if ($validate !== FALSE) { $this->check_insist(); } $this->_is_saving = TRUE; // These will be processed later $values = $defaults = array(); if ($this->meta()->events()->trigger('model.before_save', $this, array($this->_changed)) === FALSE) { return $this; } // Trigger callbacks and ensure we should proceed $event_type = $key ? 'update' : 'create'; if ($this->meta()->events()->trigger('model.before_' . $event_type, $this, array($this->_changed)) === FALSE) { return $this; } $this->_move_retrieved_to_changed(); // Iterate through all fields in original in case any unchanged fields // have convert() behavior like timestamp updating... // foreach (array_merge($this->_original, $this->_changed) as $column => $value) { if ($field = $this->meta()->field($column)) { // Only save in_db values if ($field->in_db) { // See if field wants to alter the value on save() $value = $field->convert($this, $value, $key); // Only set the value to be saved if it's changed from the original if ($value !== $this->_original[$column]) { $values[$field->column] = $value; } elseif (!$key and (!$this->changed($field->name) or $field->default === $value) and !$field->primary) { $defaults[$field->column] = $field->default; } } } } // If we have a key, we're updating if ($key) { // Do we even have to update anything in the row? if ($values) { Jam::update($this)->where_key($key)->set($values)->execute(); } } else { $insert_values = array_merge($defaults, $values); list($id) = Jam::insert($this)->columns(array_keys($insert_values))->values(array_values($insert_values))->execute(); // Gotta make sure to set this $key = $values[$this->meta()->primary_key()] = $id; } // Re-set any saved values; they may have changed $this->set($values); $this->_loaded = $this->_saved = TRUE; $this->meta()->events()->trigger('model.after_save', $this, array($this->_changed, $event_type)); $this->meta()->events()->trigger('model.after_' . $event_type, $this, array($this->_changed)); // Set the changed data back as original $this->_original = array_merge($this->_original, $this->_changed); $this->_changed = array(); foreach ($this->_retrieved as $name => $retrieved) { if ($association = $this->meta()->association($name)) { if ($association instanceof Jam_Association_Collection) { $retrieved->clear_changed(); } } elseif ($field = $this->meta()->field($name) and $field->in_db) { unset($this->_retrieved[$name]); } } $this->_is_saving = FALSE; return $this; }
<?php Jam::insert('test_element')->columns(array('id', 'name', 'url', 'email', 'description', 'amount', 'test_author_id'))->values(array(1, 'Part 1', 'http://parts.wordpress.com/', '*****@*****.**', 'Big Part', 20, '1'), array(2, 'Part 2', 'http://parts.wordpress.com/', '*****@*****.**', 'Small Part', 10, '1'))->execute(); /** * BLOGS */ $blog1 = Jam::create('test_blog', array('id' => 1, 'name' => 'Flowers blog', 'url' => 'http://flowers.wordpress.com/', 'test_posts_count' => '1')); $blog2 = Jam::create('test_blog', array('id' => 2, 'name' => 'Awesome programming', 'url' => 'http://programming-blog.com', 'test_posts_count' => '0')); $blog3 = Jam::create('test_blog', array('id' => 3, 'name' => 'Tabless', 'url' => 'http://bobby-tables-ftw.com', 'test_posts_count' => '1')); /** * Positions */ $position1 = Jam::create('test_position', array('id' => 1, 'name' => 'Staff')); $position2 = Jam::create('test_position', array('id' => 2, 'name' => 'Freelancer')); $position3 = Jam::create('test_position_big', array('id' => 3, 'name' => 'Freelancer', 'size' => 'Huge')); /** * Authors */ $author1 = Jam::create('test_author', array('id' => 1, 'name' => 'Jonathan Geiger', 'email' => '*****@*****.**', 'test_position' => $position1, 'test_blogs_owned' => array($blog1, $blog3))); $author2 = Jam::create('test_author', array('id' => 2, 'name' => 'Paul Banks', 'email' => '*****@*****.**')); $author3 = Jam::create('test_author', array('id' => 3, 'name' => 'Bobby Tables', 'email' => '*****@*****.**', 'test_position' => $position2, 'test_blogs_owned' => array($blog2))); /** * CATEGORIES */ Jam::create('test_category', array('id' => 1, 'name' => 'Category One', 'test_author' => $author1, 'test_blog' => $blog1, 'children' => array(array('id' => 3, 'name' => 'Category Three', 'test_author' => $author1, 'test_blog' => $blog2, 'children' => array(array('id' => 5, 'name' => 'Category Five')))))); Jam::create('test_category', array('id' => 2, 'name' => 'Category Two', 'is_featured' => TRUE, 'test_author' => $author1, 'test_blog' => $blog1)); Jam::create('test_category', array('id' => 4, 'name' => 'Category Four', 'is_featured' => TRUE, 'test_author' => $author1, 'test_blog' => $blog3)); /** * POSTS */ $post1 = Jam::create('test_post', array('id' => 1, 'name' => 'First Post', 'slug' => 'first-post', 'status' => 'draft', 'created' => 1264985737, 'updated' => 1264985737, 'published' => 1264985737, 'test_author' => $author1, 'test_blog' => $blog1, 'test_categories' => array(1, 2, 3)));