cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Support default value from MySQL

Open gyde opened this issue 14 years ago • 20 comments

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';
}

gyde avatar Sep 25 '12 13:09 gyde

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?

ghost avatar Sep 25 '12 17:09 ghost

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.

niden avatar Sep 25 '12 17:09 niden

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
}

gyde avatar Sep 26 '12 06:09 gyde

I like that idea :)

niden avatar Sep 26 '12 14:09 niden

gyde +1

xboston avatar Sep 26 '12 15:09 xboston

+1

nesbert avatar Sep 26 '12 16:09 nesbert

@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)?

ghost avatar Sep 30 '12 19:09 ghost

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.

gyde avatar Oct 01 '12 06:10 gyde

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")
));

hdogan avatar Oct 01 '12 18:10 hdogan

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 avatar Oct 01 '12 18:10 daviddoran

@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.

hdogan avatar Oct 01 '12 18:10 hdogan

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

bungcip avatar Oct 02 '12 12:10 bungcip

Currently, you can use:

$model->name = new Phalcon\Db\RawValue('default');
$model->current_time = new Phalcon\Db\RawValue('now()');

ghost avatar Oct 02 '12 14:10 ghost

@phalcon ups sorry, I missed that RawValue are already existed.

bungcip avatar Oct 02 '12 14:10 bungcip

A short name for "doNotRequireFields" maybe? :)

ghost avatar Oct 02 '12 19:10 ghost

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.

hdogan avatar Oct 02 '12 22:10 hdogan

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.

gyde avatar Oct 08 '12 07:10 gyde

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

hdogan avatar Oct 20 '12 16:10 hdogan

+100!

salarmehr avatar Oct 21 '12 09:10 salarmehr

0.6.0 now supports this

http://docs.phalconphp.com/en/0.6.0/reference/models.html#skipping-columns

ghost avatar Oct 25 '12 17:10 ghost