我有一个项目,目标是当用户创建一个项目时,我将在保存项目中提示输入电子邮件地址我将检查用户是否已经存在,如果不存在,我将继续并在后台创建具有一堆默认值的新用户如果用户想要激活,那么用户可以回来并完成个人资料。我怎么能在模型中做到这一点。似乎唯一的保存方法是传入数组我如何使用对象参数来做到这一点。
App::uses('User', 'Model');
class Item extends AppModel{
public function save($data = null, $validate = true, $fieldList = array()) {
$user = User::getUserbyEmail($data['Item']['email_address']);
if($user){
$user = new User();
$user->firstName = "Bob";
$user->save(); /// this save does not work
}
//get user Id and call parent save
.........
}
class User extends AppModel {
public $firtName;
private $created;
private $status = 0; //0 is in active
$private $password;
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->created = date("Y-m-d H:i:s");
$this->setPassword($password);
}
public function setPassword($value){
$this->password = mysecret_algorithm(standard_password);
}
..bunch of setter and getter here
}
我正在使用 cakephp,但我不想在控制器中执行此操作,因为我在多个地方都有项目添加功能,在模型中这样做会很好,然后每个控制器只需调用 $this->Item->save();