I'm fairly new to ORM, but what I am trying to do is insert a new comment in to the MySQL database and update users table.
So I have two tables, users
and comments
.
Now when i call save()
method on comments object i would like to insert that comment in to comments
table and also update user written_comments
column in users
table.
The part where I only need to insert comment is easy and I've already done it.
Problem comes with the update query where i need to update users
table and set written_comments + 1 where id = $userId
I have found documentation about relations between objects, but i can't figure this out.
I am using Kohana 3.3 version.
My comments
class right now is empty:
class Model_Comments extends ORM{
}
And I'm extending Auth user class
class Model_User extends Model_Auth_User {
}
Any help at all would help a lot, thanks.
If you want use only ORM, try write something like this after saving a comment
$user = ORM::factory('User', $userId);
if($user->loaded()){
$user->set('written_comments', $user->written_comments + 1)->update();
}
There are no way to bind callback on ORM::save(). You can only check saving a comment into database with ORM::loaded() method.