【问题标题】:Cakephp - Conditional SaveCakephp - 条件保存
【发布时间】:2012-03-12 17:32:10
【问题描述】:

我的表单包含一个模型对象,其中包含五个使用 hasMany 关联的子对象。当我保存表单时,我注意到所有字段,无论它们是否为空,都保存到数据库中。是否可以在 beforeSave() 回调方法中设置条件以防止没有值的子项被保存?我试图取消设置包含空值的数组中的键,但该行仍在添加到数据库中。

这是我的“Mtd”模型的代码。 Mtd 模型包含许多 Flowratereatments。在我的表格上,我有一个复选框,上面写着“这是基于流量的治疗”。因此,如果用户单击它,则用户可以将其填写在字段中。但是,如果用户不填写,我想防止仅使用 Mtd 表的外键添加新行。

<?php
class Mtd extends AppModel {
   public $name = 'Mtd';
   public $hasOne = array('Treatmentdesign', 'Volumetreatment');
   public $hasMany = 'Flowratetreatment';


   function beforeSave() {
      if($this->data['Mtd']['is_settling'] != 1){
         unset($this->data['Flowratetreatment'][0]);
      } 
      return true;
   }
}

?>

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    您是否尝试过类似的方法:

    class User extends AppModel {
        function validates() {
            $this->setAction();
            #always validate presence of username
            $this->validates_presence_of('username');
            #validate uniqueness of username when creating a new user
            $this->validates_uniqueness_of('username',array('on'=>'create'));
            #validate length of username (minimum)
            $this->validates_length_of('username',array('min'=>3));
            #validate length of username (maximum)
            $this->validates_length_of('username',array('max'=>50));
            #validate presence of password
            $this->validates_presence_of('password');
            #validate presence of email
            $this->validates_presence_of('email');
            #validate uniqueness of email when creating a new user
            $this->validates_uniqueness_of('email',array('on'=>'create'));
            #validate format of email
            $this->validates_format_of('email',VALID_EMAIL);
    
            #if there were errors, return false
            $errors = $this->invalidFields();
            return (count($errors) == 0);
        }
    }
    ?> 
    

    在你的模型中

    【讨论】:

    • 感谢您的快速响应。对不起,我觉得我的问题有点不清楚。我不是要验证信息。我想要做的是“如果与 ModelA 有关的字段为空,则不要在数据库中添加一行”。我不想要求用户填写这些字段。这些将是可选字段,如果填写,则应保存。否则,它们不应该是。
    • 你不能使用 cakephp 创建一个事务,所以如果任何数据产生问题你创建一个回滚
    【解决方案2】:

    我用过这段代码:

    public function beforeSave() {
        if(isset($this->data[$this->alias]['profile_picture'])) {
            if($this->data[$this->alias]['profile_picture']['error']==4) {
                unset($this->data[$this->alias]['profile_picture']);
            }
        }
        return true;
    }
    

    在以前的应用程序中,如果用户没有上传文件,则从$this-&gt;data 中删除一个键,以防止旧值被覆盖。

    这应该适合你(你需要适应它;基于 $this-&gt;data 此时包含的内容。

    public function beforeSave() {
        if(empty($this->data[$this->alias]['the_key'])) {
            unset($this->data[$this->alias]['the_key']);
        }
        //debug($this->data); exit; // this is what will be saved
        return true;    
    }
    

    你提到你已经尝试过了?在原始帖子中发布您的代码。

    【讨论】:

    • 我发布了我的代码。我正在检查复选框的值。如果它没有被选中,那么我想取消设置该部分具有的字段的值。然而,尽管我取消了设置,但还是添加了一行,其中仅包含 Mtd 表的外键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多