如何在模型中保存cakephp对象

如何在模型中保存cakephp对象,cakephp,model,save,Cakephp,Model,Save,我有一个项目,目标是当用户创建一个项目时,我将在保存项目中提示输入电子邮件地址,我将检查用户是否已经存在,如果不存在,我将继续在后台创建一个具有默认值的新用户,如果用户想要激活,则用户可以返回并完成配置文件。我怎样才能在模型中做到这一点。似乎保存的唯一方法是传入数组,如何使用对象参数来实现这一点 App::uses('User', 'Model'); class Item extends AppModel{ public function save($data = null, $valid

我有一个项目,目标是当用户创建一个项目时,我将在保存项目中提示输入电子邮件地址,我将检查用户是否已经存在,如果不存在,我将继续在后台创建一个具有默认值的新用户,如果用户想要激活,则用户可以返回并完成配置文件。我怎样才能在模型中做到这一点。似乎保存的唯一方法是传入数组,如何使用对象参数来实现这一点

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,我不想在控制器中执行此操作,因为我在多个位置具有item add功能,最好在模型中执行此操作,然后每个控制器只调用$this->item->save()

在您的代码中,
User::getUserbyEmail
的用途是什么

要保存用户,请尝试以下操作:

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(); // EDIT: forgot this part
           $user->create();
           $user->set($userData);
           $user->save();  /// this save does not work
       }
   }
   //get user Id and call parent save
   .........
}
上面的
$userData
应该是一个关联数组,其中数组键是用户数据库表中字段的名称:

$userData = array(
    'firstname' => 'Bob'
);
请注意,在这种情况下,您的代码必须通过验证