I have looked at the other questions concerning this error message, but all appear to have something to do with a miss match between a parameter and its binding, or missing bindings, or something like that. Here is my code. Can anyone here see the error? This is in a CDbMigration class.
$this->insert('company', array(
'name' => 'Site Administrator',
));
$companyId = $this->dbConnection->getLastInsertID();
$sql = 'insert into `user` (`company-id`, `login-id`, `password`) ';
$sql .= 'values (:company-id, :login-id, :password)';
$command = $this->dbConnection->createCommand($sql);
$command->bindValue(':company-id', $companyId, PDO::PARAM_INT);
$command->bindValue(':login-id', 'admin');
$command->bindValue(':password', 'admin');
$command->execute();
The first insert performs properly, the second gives the following error:
Exception: CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was: insert into `user` (`company-id`, `login-id`, `password`) values (:company-id, :login-id, :password). Bound with :company-id='6', :login-id='admin', :password='admin' (C:\eclipse-php-3.0.2\workspace\interview\lib\yii\db\CDbCommand.php:357)
For the record, I realize that I need to do something to the password so that it is not stored in plain text. I will deal with that later. In addition, the user table is defined in the migration class as follows:
$this->createTable('user', array(
'id' => 'pk',
'company-id' => 'integer not null',
'login-id' => 'string unique not null',
'password' => 'string not null',
'full-name' => 'string',
'mobile-phone' => 'string',
'other-phone' => 'string',
'email' => 'string unique',
));
$this->addForeignKey('fk_user_company', 'user', 'company-id', 'company', 'id'
, 'restrict');
Why not continue in the same way and have:
$this->insert('user', array(
'company-id' => (int)$companyId,
'login-id' => 'admin',
'password' => 'admin'
));
?
Also, using dashes instead of underscores is not the best idea you have had for a table column name. Later you'll want to use CActiveRecord to reference the login id for example, but
$model->login-id;
is invalid so you'd have to
$model->{'login-id'} // or
$model->getAttribute('login-id');
which is a pain. Overall i advise you to switch it to underscore.
According to Yii documentation:
SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
So:
$command->bindValue(':company-id', $companyId);
Or
$command->bindValue(':company-id', (int)$companyId);
Should works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.