本文整理汇总了PHP中Jam::insert方法的典型用法代码示例。如果您正苦于以下问题:PHP Jam::insert方法的具体用法?PHP Jam::insert怎么用?PHP Jam::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jam
的用法示例。
在下文中一共展示了Jam::insert方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_insert
/**
* @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());
}
示例2: save
/**
* 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;
}
示例3: array
<?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/', 'staff@example.com', 'Big Part', 20, '1'), array(2, 'Part 2', 'http://parts.wordpress.com/', 'staff@example.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' => 'jonathan@jonathan-geiger.com', 'test_position' => $position1, 'test_blogs_owned' => array($blog1, $blog3)));
$author2 = Jam::create('test_author', array('id' => 2, 'name' => 'Paul Banks', 'email' => 'paul@banks.com'));
$author3 = Jam::create('test_author', array('id' => 3, 'name' => 'Bobby Tables', 'email' => 'bobby@sql-injection.com', '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)));