Reset MySQL default values programmatically
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.
精彩评论