Cakephp-条件保存

Cakephp-条件保存,php,cakephp,Php,Cakephp,我的表单包含一个模型对象,该模型对象包含五个子对象,这些子对象使用hasMany进行关联。当我保存表单时,我注意到所有字段(无论是否为空)都保存到数据库中。是否可以在beforeSave()回调方法中设置一个条件,以防止保存没有值的子项?我已尝试取消设置包含空值的数组中的键,但该行仍在添加到数据库中 这是我的“Mtd”模型的代码。Mtd模型包含许多流量处理。在我的表格上,我有一个复选框,上面写着“这是一种基于流速的治疗”。因此,如果用户单击它,那么用户可以在字段中填充它。但是,如果用户不填写它,

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

这是我的“Mtd”模型的代码。Mtd模型包含许多流量处理。在我的表格上,我有一个复选框,上面写着“这是一种基于流速的治疗”。因此,如果用户单击它,那么用户可以在字段中填充它。但是,如果用户不填写它,我希望防止仅使用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;
   }
}

?>

您是否尝试过以下方法:

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);
    }
}
?> 

在您的型号中,您是否尝试过以下方法:

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);
    }
}
?> 
在您的型号中

我使用了以下代码:

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->data
中删除密钥,以防止覆盖旧值

这应该适合您(您需要根据
$this->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;    
}
你提到你已经试过了吗?在你原来的帖子中发布你的代码。

我使用过这个代码:

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->data
中删除密钥,以防止覆盖旧值

这应该适合您(您需要根据
$this->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;    
}

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

谢谢你的快速回复。对不起,我认为我的问题有点不清楚。我不想验证信息。我想做的是“如果与ModelA相关的字段为空,那么不要在数据库中添加行”。我不想要求用户填写这些字段。这些字段是可选字段,如果填写,应该保存。否则,它们不应该保存。您不能使用cakephp创建事务吗?因此,如果任何数据产生问题,您可以创建回滚。感谢您的快速响应。抱歉,我认为我的问题有点不清楚。我不想尝试这样做验证信息。我试图做的是“如果与ModelA相关的字段为空,则不要在数据库中添加行”。我不想要求用户填写这些字段。这些字段是可选字段,如果已填写,则应保存。否则,它们不应保存。您不能使用cakephp创建事务吗?因此,如果任何数据产生问题,请创建回滚。我发布了我的代码。我正在检查复选框的值。如果未选中,则我不希望o取消设置该节具有的字段的值。但是,尽管我取消设置,仍会添加一行,其中仅包含Mtd表的外键。我发布了代码。我正在检查复选框的值。如果未选中该复选框,则我希望取消设置该节具有的字段的值。但是,尽管我取消设置,仍会添加一行仅包含Mtd表的外键。