Both links http://fueldevdocs.exite.eu/classes/model_crud/methods.html#/method_pre_save and http://docs.fuelphp.com/classes/model_crud/methods.html#/method_pre_save have the same example code:
// Inside your model
protected function pre_save(&$query)
{
// change the query
$query->set(array('field' => 'value'));
}
My model looks like this:
class Model_Experiment extends \Model_Crud
{
protected static $_table_name = 'experiment';
protected static $_primary_key = 'id';
protected static $_rules = array(
'name' => 'required|trim|min_length[3]|max_length[512]'
);
protected function pre_save(&$query)
{
$query->set(array(
'created_on' => Date::forge()->format('%Y-%m-%d')
));
}
}
My controller looks like this:
public function action_add()
{
$fieldset = Fieldset::forge();
$fieldset->form()->add('name', 'Name:', array('type' => 'text', 'value' => $fieldset->validation()->input('name')), array('rules' => array('required')))->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 512)->add_rule('trim');
$fieldset->form()->add('submit', '', array('type' => 'submit', 'value' => 'Add'));
if( $fieldset->validation()->run() === true )
{
Model_Experiment::forge()->set(array(
'name' => $fieldset->validation()->validated('name'))
)->save();
}
}
The problem is that pre_save method should return the $query, which indicates this error message when I open page that executes this code.
ErrorException [ Error ]: Call to a member function execute() on a non-object
422 $query = $this->pre_save($query);
423 $result = $query->execute(isset(static::$_connection) ? static::$_connection : null);
424
and also this non-fatal error:
Runtime Notice: Declaration of Model_Experiment::pre_save() should be compatible with that of Fuel\Core\Model_Crud::pre_save()
so I believe proper definition of pre_save method is:
// Inside your model
protected function pre_save($query)
{
// change the query
$query->set(array('field' => 'value'));
return $query;
}
but even If I correct that I get sql error:
INSERT INTO `experiment` (`name`, created_on`) VALUES ('fgdfd'), ('2012-05-16');
the problem is that after VALUES there are two () ().
Current docs (and dev-docs) document v1.2.
Comparing the code, the only difference i can find is that in the 1.1 release, the parameter was passed by reference. In 1.2, it is passed by value, and the possibly updated query is returned.
I don't use Model_Crud myself, so I can't help you any further.