views:

203

answers:

2

Hi,

Can anyone help me with Kohana ORM. I can take out name and value. I can give them new values and I try to save them back to base, but in phpmyadmin i can see still old values for these option attributes. What is wrong with this code (it works and echos right value but i can't see it in db):

$option = ORM::factory('draft')
->where('user_id', '=', $user_id)
->find()
    ->draft_options
    ->where('name', '=', $_POST['name'])
    ->find();

$option->name = $_POST['name'];
$option->value = $_POST['value'];
$option->save();
if ($option->saved()) echo Kohana::debug($option->value);
+1  A: 

Try checking does the data get loaded with "$option->loaded()", or echo the $option ( it'll return you it's primary key ) after you "find()" it please.

Kemo
$option->loaded() echos TRUE and option echos object info (Model_Draft_option(35) etc ..). Firstly if i echo $option->value then its value is 1. Then after I change $option->value = 2; $option->save(); then it's still not change the value.
Bob0101
are the name and value fields belonging to table drafts or draft_options ? because calling save() on one model will not save belonging models unless you override the save() method and tell it to do so.
Kemo
these belong to draft_options. how can i override it?
Bob0101
+1  A: 

Is this what you are looking for?

$option = ORM::factory('draft')
    ->where('user_id', '=', $user_id)
    ->find();

$draft_option = $option->draft_options
    ->where('name', '=', $_POST['name'])
    ->find();

$draft_option->name = $_POST['name'];
$draft_option->value = $_POST['value'];
$draft_option->save();

if ($draft_option->saved()) echo Kohana::debug($draft_option->value);
slacker
i think your solution is corrent, but in my case, it's still not working. i think there must me something wrong with the model.
Bob0101

related questions