【问题标题】:CakePHP - Set Default Field Values in the ModelCakePHP - 在模型中设置默认字段值
【发布时间】:2011-12-01 15:51:40
【问题描述】:

如何为模型中的字段设置默认值?

编辑:

我已经按照建议尝试了使用 _schema 的方法,但没有使用默认值。

public $_schema = array(
    'newsletter' => array(
        'default' => 1
    ),
);  

【问题讨论】:

  • 是什么阻止您在数据库中执行此操作?
  • 我已经尝试过链接中描述的方法,但似乎无法正常工作。
  • @freshest 哪种方法?在 Google 上搜索?
  • @elitalon _schema 方法。

标签: cakephp


【解决方案1】:

您应该始终尝试从控制器设置默认值: http://www.dereuromark.de/tag/default-values/

【讨论】:

  • 对于最近的访问者:请务必向下滚动 Mark 的说明以查看他的 CakePHP 2.x 和 3.x 更新。
  • 默认值应该在模型中设置你不觉得吗?可能在 beforesave 方法中,否则您有多个设置默认值的控制器操作
【解决方案2】:

在数据库中设置默认值会更好吗?我真的不明白你为什么要在 CakePHP 方面这样做......

【讨论】:

  • 这样应用是可移植的,目前我们还不知道应用将使用什么数据库。
  • OP 的理由是合法的 - 但感谢 @jwg2s - 这是一个巨大的帮助。
【解决方案3】:

由于上述建议对我不起作用,所以我找到了自己的建议。答案与上面写的非常相似,但稍作修正。 (适用于 CakePHP 2.6.1)

默认值可以在控制器的add函数中设置(需要“request”)。

$this->request->data['Country']['hasFlag'] = 1; 

完整代码示例:

public function add() {
    if ($this->request->is('post')) {
        $this->Country->create();
        if ($this->Country->save($this->request->data)) {
            ...
        } else {
            ...
        }
    }
    $this->request->data['Country']['hasFlag'] = 1;  // default value passing to the view
}

一些哲学:
1) 为什么需要这样做 - 如果我们在数据库中有布尔属性,Cakephp 中新创建的对象不会考虑数据库中的帐户默认值。如果我们在新对象的 Add 表单中未选中复选框并将其提交到数据库 - 这意味着此属性值为 false(不是 value not set
2) 这是设置默认值的理想位置吗? - 不,这不是理想的位置,因为有关对象及其数据的所有信息都必须在模型中,但我没有管理在模型中分配默认值。即使使用 _schema 变量或 create 函数。

【讨论】:

  • 这从下面完成了我的答案。
【解决方案4】:

您可以在数据库中设置值并在架构中对其进行管理,例如:

public $items = array(
    'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 10, 'unsigned' => false, 'key' => 'primary'),
    'quantity' => array('type' => 'decimal', 'null' => false, 'default' => '1.00', 'length' => '12,2', 'unsigned' => false),
    //                                                        ^^^^^^^^^^^^^^^^^^^
    'indexes' => array(
        'PRIMARY' => array('column' => 'id', 'unique' => 1),
    ),
    'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_spanish_ci', 'engine' => 'InnoDB')
);

稍后可以通过模型的schema 属性在模型或控制器中读取此默认值:

// Controller example
$itemSchema = $this->Item->schema();
$defaultQuantity = $itemSchema['quantity']['default'];
// ... or:
$quantityInfo = $this->Item->schema('quantity');
$defaultQuantity = $quantityInfo['default'];

在最近的 PHP 版本中,它可以是单行的:

$defaultQuantity = $this->Item->schema('quantity')['default'];

这适用于带有 MySQL 适配器的 Cake/2.5(不知道其他场景)。

【讨论】:

    【解决方案5】:

    数据库变得很大,因此您无法记住您设置的所有默认值。让我们保持简单:

    1. 您在控制器中设置默认值(如果某些操作的默认值在开头设置为类属性,则代码很容易阅读)。

    例如:

    class UsersController extends AppController {
    private $registerDefaults = array(
        'group_id' => '1'
    );
    
    public function register() {
        if ($this->request->is('post')) {
            /*
            * This is where you set default value
            * Here's what I do for default group that user should be assigned to
            */
            $this->request->data['User']['group_id'] = $this->registerDefaults['group_id'];
    
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('You have been successfully registered.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('We're unable register this user.'));
        }
    }
    }
    

    如果您有大约 60 到 80 个具有复杂关系的表,您可能无法始终记住数据库中设置的默认值。

    我的建议是不要设置依赖于当前设置的默认值,要更加灵活:创建配置表或在 AppController 中设置默认值以便眨眼间找到它。

    【讨论】:

    • 而且,如果默认值改变了怎么办?如果默认值现在无效怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2015-06-30
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多