gpt4 book ai didi

CakePHP 2.2 saveAll 多模型验证

转载 作者:行者123 更新时间:2023-12-01 14:19:55 33 4
gpt4 key购买 nike

我正在尝试在 saveAll 上验证多个模型。第一个模型中的验证被触发,但是当涉及到相关模型时,似乎没有任何反应。我什至试图通过退出来检查 beforeValidate() 和 beforeSave() 方法是否被调用;在它们中,但代码继续正常执行。

联系方式模型:

<?php
class ContactDetails extends ContactAppModel {
public $actsAs = array("MapValidate");
public $hasMany = array(
'ProjectLocation' => array(
'className' => 'ProjectLocation',
'foreignKey' => 'project_id'
)
);

public $validate = array(
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact name is required'
)
),
'address1' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact address 1 is required'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact email is required'
),
'email' => array(
'rule' => array('email'),
'message' => 'Contact email format is not valid'
)
)
);
}

项目位置模型:

<?php
class ProjectLocation extends ContactAppModel {
public $actsAs = array("MapValidate");
public $belongsTo = array(
"ContactDetails" => array(
"className" => "ContactDetails",
"foreignKey" => "project_id"
);
);

public $validate = array(
'lat' => array(
'checkLocation' => array(
'rule' => array('checkMap', 'lat'),
'message' => 'One or more positions on the map are invalid.'
)
)
);
}

这是我要保存的 $this->request->data:

Array
(
[ContactDetails] => Array
(
[id] => 1
[name] => PreeoStudios
[address1] => 4, Stivala Street
[address2] => Mosta, MST 3205
[address3] => Malta
[telephone] => 34562737
[email] => info@preeostudios.com
[fax] => N/A
[skype] => N/A
)

[ProjectLocation] => Array
(
[0] => Array
(
[lat] => 35.886277456343024
[lon] => 14.428907312499973
)

[1] => Array
(
[lat] => 35.886277456343024
[lon] => 14.528907312499973
)

)

)

saveAll 调用:

$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first'))

编辑

我还尝试从关联模型中删除验证规则并在 beforeSave 函数中放置一个退出...代码一直在执行

<?php
class ProjectLocation extends ContactAppModel {
public $actsAs = array("MapValidate");
public $belongsTo = array(
"ContactDetails" => array(
"className" => "ContactDetails",
"foreignKey" => "project_id"
);
);

public function beforeSave(){
exit;
}

public $validate = array(

);
}

最佳答案

尝试添加 deep 选项:

$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first', 'deep' => true))

发件人:http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array

关于CakePHP 2.2 saveAll 多模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13542493/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com