【问题标题】:CakePhp insert issue using Postgresql database使用 Postgresql 数据库的 CakePhp 插入问题
【发布时间】:2013-10-18 13:56:21
【问题描述】:

我对 cakephp 有这个问题我有一个表用户和配置文件,
profile 表有一个外键 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.'
            )
        )
    );
}


这里是Schema

【问题讨论】:

    标签: cakephp cakephp-2.3


    【解决方案1】:

    您的字段可能是数组类型。你能发布你的数据库架构吗?

    第 42 条和第 43 条似乎是错误的。时间戳数组?

    【讨论】:

    • 我已经更新了我上面的问题,我已经包含了用户和配置文件模型以及链接到 Pastebin 的架构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2019-03-08
    • 2017-05-15
    相关资源
    最近更新 更多