我对 cakephp 有这个问题我有一个表用户和配置文件,
配置文件表有一个外键 user_id,它引用用户表中的 id 列
现在当我插入时,我首先为用户插入数据,然后我从用户表中检索最后插入数据的 id,然后将其传递给配置文件表中的 user_id 字段,现在问题是我收到此错误:
错误:SQLSTATE[22P02]:无效的文本表示:7 错误:数组值必须以“{”或维度信息开头
这是数据插入的代码。
/*
* signup method
* @return void
*/
public function signup(){
if($this->request->is('post')){
$this->User->create();
//Tweek to unset modified field.
$user_data = array(
'username' => $this->request->data['User']['username'],
'password' => $this->request->data['User']['password'],
'modified' => false
);
$user = $this->User->save($user_data);
if(!empty($user)){
//If the user was saved get the users id and pass to profile user_id foreign key
$this->request->data['Profile']['user_id'] = $this->User->id;
// Because our User hasOne Profile, we can access
// the Profile model through the User model
$profile_data = array(
'user_id' => $this->request->data['Profile']['user_id'],
'student_id' => $this->request->data['Profile']['student_id'],
'first_name' => $this->request->data['Profile']['first_name'],
'middle_name' => $this->request->data['Profile']['middle_name'],
'last_name' => $this->request->data['Profile']['last_name'],
'modified' => false
);
if($this->User->Profile->save($profile_data)){
$this->Session->setFlash(__('Your Account has been successfully added.'));
return $this->redirect(array('action' => 'login'));
}
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array(), 'register_error');
}
}
$this->layout = 'custom_layouts/default';
$data = array(
'id' => 'signup',
'title_for_layout' => 'Reservation . Sign up'
);
$this->set($data);
}
这是用户模型:
<?php
App::uses('AuthComponent', 'Controller/Component');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
/**
* User Model
*
*/
class User extends AppModel {
//Allow Model to use both users and profiles table
public $uses = array('User', 'Profile');
//Setting Models Association
public $hasOne = array(
'Profile' => array(
'classname' => 'Profile',
'order' => array('Profile.last_name' => 'ASC'),
'dependent' => TRUE
)
);
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
),
'minlength' => array(
'rule' => array('minlength', '8'),
'message' => 'Password should be 8 characters long'
)
)
);
public function beforeSave($options = array()){
//Hash passwords before saving to database
if(!empty($this->data[$this->alias]['password']) || isset($this->data[$this->alias]['password'])){
$hashedPassword = Security::hash($this->data[$this->alias]['password'],"blowfish");
$this->data[$this->alias]['password'] = $hashedPassword;
}
return true;
}
}
这是配置文件模型:
<?php
/**
* User Model
*
*/
class Profile extends AppModel {
//Setting Models Association
public $belongsTo = array(
'User' => array(
'classname' => 'User',
'foreignKey' => 'user_id'
)
);
//Data Validation
public $validate = array(
'student_id' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A student id is required'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This student id has already been taken.'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'An email is required'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This email address has already been taken.'
)
)
);
}
这是架构