Support default value from MySQL
If im doing it right, and as far as the documentation goes right now only field which allows NULL value is allow to be excluded when saving using a model.
MySQL has a default value parameter which would be optimal to use, and therefore avoid the 'require fieldname' error.
To solve this problem now, I have added this to my _preSave where needed:
public function preSave ()
{
if (!isset($this->field_name))
$this->field_name = 'default_value';
}
I'm not sure what could be the best behavior in this case:
$robot->name = null; // this must store null or the column's default?
That might not work.
Scenario 1:
field = has_data nullable = true default value = 4
Setting
$field->has_data = null;
will not assign the default value.
I propose the creation of a constant, something like PHALCON_COLUMN_DEFAULT_VALUE or whatever, which would effectively "remove" that column from the save/update process so that the underlying RDBMS can assign the default value itself.
Thoughts?
Nikos
The contents of this message may contain confidential or privileged information and is intended solely for the recipient(s). Use or distribution to and by any other party is not authorized. If you are not the intended recipient, copying, distribution or use of the contents of this information is prohibited.
On Tue, Sep 25, 2012 at 1:36 PM, Phalcon [email protected] wrote:
I'm not sure what could be the best behavior in this case:
$robot->name = null; // this must store null or the column's default?
— Reply to this email directly or view it on GitHubhttps://github.com/phalcon/cphalcon/issues/109#issuecomment-8863022.
Scenatio 1:
Correct, that will not work, if a field is nullable and you assign NULL even tho it has a standard default value the field value will be NULL.
Don't see the need for a constant, since describe is used to get info about a table and its fields, fields with default value can just be excluded from the insert / update statements inorder to avoid the required error?
This problem also applies if you have a field where you use default value = CURRENT_TIMESTAMP and attribute ON UPDATE CURRENT TIMESTAMP. (This feature is widely used and should be supported) Which should always be set to the NOW() of the MySQL server and not depend on the executing webserver.
The solution of course depends on what you expect the Model to handle for you vs the RDBMS.
A function like, doNotRequireFields($fields = array('field_name','field_name')) called in model initialize would also work, a bit like my own solution to the problem. This would just use the RDBMS default value instead of the one I supplied.
public function initialize()
{
$this->doNotRequireFields(array('field_name_1','field_name_2'));
//Other initialize stuff
}
I like that idea :)
gyde +1
+1
@gyde A general option is fine, but what if sometimes I want the default value and other times and I want to set a specific value (don't use the default)?
If your using the doNotRequireFields idear, then its just a matter of setting the value if you want to use a specific value, if a value is set on a default value field, then send it along with the query, if not set, then do not send it.
maybe a whitelist/blacklist option for save method? for example:
/* just skip field_name_1 and field_name_2 fields. */
$myModel->save(array(
"skip" => array("field_name_1", "field_name_2")
));
/* only allow to save field_name_1 and field_name_2 fields. */
$myModel->save(array(
"allow" => array("field_name_1", "field_name_2")
));
Is this something other ORMs have solved? E.g. Doctrine, ActiveRecord, etc. Might be a good idea to take lessons from them.
On 1 Oct 2012, at 19:22, Hidayet Doğan wrote:
maybe a whitelist/blacklist option for save method? for example:
/* just skip field_name_1 and field_name_2 fields. */ $myModel->save(array( "skip" => array("field_name_1", "field_name_2") ));
/* only allow to save field_name_1 and field_name_2 fields. */ $myModel->save(array( "allow" => array("field_name_1", "field_name_2") )); — Reply to this email directly or view it on GitHub.
@daviddoran If I remember right, CakePHP has an option to pass only allowed fields list (I don't remember exact option name). And Lithium Framework has "whitelist" option.
in yii framework you can use sql expression as the value
$model->name = new CDbExpression('default');
$model->current_time = new CDbExpression('now()');
maybe phalcon can use that idea
Currently, you can use:
$model->name = new Phalcon\Db\RawValue('default');
$model->current_time = new Phalcon\Db\RawValue('now()');
@phalcon ups sorry, I missed that RawValue are already existed.
A short name for "doNotRequireFields" maybe? :)
skipFields?
Hidayet Doğan (iPhone'umdan) http://hi.do
2 Eki 2012 tarihinde 22:44 saatinde, Phalcon [email protected] şunları yazdı:
A short name for "doNotRequireFields" maybe? :)
— Reply to this email directly or view it on GitHub.
Phalcon\Db\RawValue('default') suits my purpose for now :)
A doNotRequireFields (or shorter name) would be handy.
Oops, hit the close button, not on purpose, but can't delete that entry.
I think this is an upcoming feature with 0.6.0, a new shiny skipAttributes method:
https://github.com/phalcon/cphalcon/blob/0.6.0/ext/mvc/model.c#L3412
+100!
0.6.0 now supports this
http://docs.phalconphp.com/en/0.6.0/reference/models.html#skipping-columns