开发者

Reset MySQL default values programmatically

开发者 https://www.devze.com 2023-03-17 23:00 出处:网络
Is it possible to set default values for MySQL columns in CakePHP? I have access default value for user table as:

Is it possible to set default values for MySQL columns in CakePHP? I have access default value for user table as:

$fields = $this->User->schema(); 
$defaultValue = $fields['Amount']['default'];

Now I want to change this default value to any particular value provi开发者_开发百科ded by admin .. Is it possible ?


Try altering the schema:

ALTER TABLE dbo.table
ADD CONSTRAINT def_mycolumn DEFAULT Amount FOR mycolumn


You should not use the MySQL table schema's default column attribute to store a value you plan to allow user(s) to modify.

You might consider some new DefaultValues table/model (as one solution) used to store the editable default for this Amount column and other similar columns. You could then conditionally check if a User instance's Amount has a null value when saved, setting it to the editable default retrieved from this new table. This provides you with the functionality you're looking for (admin-editable defaults) while being able to take advantage of Cake's built-in tables/models/forms to manage the storage/retrieval of these defaults.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号