CakePHP自动用另一个值填充一个字段

时间:2013-03-26 15:04:46

标签: php cakephp foreign-keys fullcalendar


我正在使用Cake 2.3.1。
在我的活动/添加视图中,我有这个字段:

echo $this->Form->input('customer_id', array('label' => 'Customer: '));

这是一个输入选择表单,其中包含我的所有客户。 现在我需要这个:

echo $this->Form->input('title', array('type' => 'hidden'));

将自动填写我在上面的字段中选择的客户名称。
怎么办呢?

关系:

  

客户HasMany事件(外键 - > customer_id)

1 个答案:

答案 0 :(得分:3)

在根据选择更改的表单中设置隐藏值并不是一个好主意。由于最终用户可以轻松地更改其价值或者可能完全禁用Javascript(这是实时更改所必需的),因此它不值得信任。在事件模型的beforeSave方法中保存表单时获取标题会更好。例如:

function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['customer_id'])) {
        // Set the Customer.name field as Event.title
        $this->data[$this->alias]['title'] = $this->Customer->field(
            'name', array(
                // The condition for the find operation on this customer
                'id' => $this->data[$this->alias]['customer_id']
             )
        );
    }

    return true;
}
相关问题