我使用 Yii 的活动记录模式已经有一段时间了。现在,我的项目需要访问不同的数据库来进行一项小事务。我认为 Yii 的 DAO 对此很有帮助。但是,我遇到了一个神秘的错误。
CDbCommand 执行 SQL 语句失败:SQLSTATE[HY093]:参数号无效:参数未定义
这是我的代码:
public function actionConfirmation
{
$model_person = new TempPerson();
$model = $model_person->find('alias=:alias',array(':alias'=>$_GET['alias']));
$connection=Yii::app()->db2;
$sql = "INSERT INTO users (username, password, ssn, surname
, firstname, email, city, country)
VALUES(:alias, :password, :ssn, :surname
, :firstname, :email, :city, :country)";
$command=$connection->createCommand($sql);
$command->bindValue(":username", $model->alias);
$command->bindValue(":password", substr($model->ssn, -4,4));
$command->bindValue(":ssn", $model->ssn);
$command->bindValue(":surname", $model->lastName);
$command->bindValue(":firstname", $model->firstName);
$command->bindValue(":email", $model->email);
$command->bindValue(":city", $model->placeOfBirth);
$command->bindValue(":country", $model->placeOfBirth);
$command->execute();
$this->render('confirmation',array('model'=>$model));
}
这将构造以下查询(如应用程序日志中所示):
INSERT INTO users (username, password, ssn, surname, firstname, email
, city, country)
VALUES(:alias, :password, :ssn, :surname, :firstname, :email, :city, :country);
FYI $model->placeOfBirth
应该是城市和县的值。这不是一个错字(只是我必须做的一件愚蠢的事情)。