我已经查看了有关此错误消息的其他问题,但似乎都与参数与其绑定之间的不匹配或缺少绑定或类似的事情有关。这是我的代码。这里的任何人都可以看到错误吗?这是在 CDbMigration 类中。
$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();
第一个插入正确执行,第二个给出以下错误:
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)
作为记录,我意识到我需要对密码做一些事情,这样它就不会以纯文本形式存储。我稍后会处理。另外,在迁移类中定义用户表如下:
$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');