user_id | message_id | is_group (bool) | unread (bool)
$msg =
ORM::factory('message_user', array(
'message_id' => $this->p('id'),
'user_id' => $_SESSION['user_id']
));
$msg->unread = 0;
$msg->save();
UPDATE `messages_users` SET `unread` = 0 WHERE `message_id` = '1'
class Model_Message_User extends adminorm {
protected $_primary_key = 'message_id';
protected $_updated_column = null;
protected $_created_column = null;
protected $_table_name = 'messages_users';
protected $_belongs_to = array('message' => array(), 'user' => array(), 'role' => array());
}
/**
* Finds and loads a single database row into the object.
*
* @chainable
* @param mixed primary key
* @return ORM
*/
public function find($id = NULL)
{
if ( ! empty($this->_load_with))
{
foreach ($this->_load_with as $alias)
{
// Bind relationship
$this->with($alias);
}
}
$this->_build(Database::SELECT);
if ($id !== NULL)
{
// Search for a specific column
$this->_db_builder->where($this->_table_name.'.'.$this->_primary_key, '=', $id);
}
return $this->_load_result(FALSE);
}
if ($id !== NULL)
{
if (is_array($id)){
foreach ($id as $column => $value){
$this->_db_builder->where($this->_table_name.'.'.$column, '=', $value);
}
} else {
// Search for a specific column
$this->_db_builder->where($this->_table_name.'.'.$this->_primary_key, '=', $id);
}
}
$query = DB::update($this->_table_name)
->set($data)
->where($this->_primary_key, '=', $this->pk()) // Updating just one key?! No idea how to proceed here
->execute($this->_db);
ORM::factory('message', $this->p('id'))->add('users', ORM::factory('user', $_SESSION['user_id']), array('unread' => 0))->save();ORM::factory('user', $_SESSION['user_id'])->add('messages', ORM::factory('message', $this->p('id')), array('unread' => 0))->save();Compound keys are probably not getting added any time soon (if ever). If you need to select the pivot table, then make a model for it, create a one to many relationship on both sides, and add an auto incrementing id to the middle table. If that's too much work for you, then simply create custom methods in your models and use the query builder manually.
Edit: If your pivot table has data other than the pivot data, then stop thinking of it as a pivot table. It's just a normal table with 2 one-to-many relationships going to other tables.
@Zeelot3k exactly what you are saying! I don't understand the trouble here? Extra data in a pivot table isn't a pivot table.. create a new model instead :)
So your modelling is:
It looks like you're new here. If you want to get involved, click one of these buttons!