如何修复Yii中的错误CDbException?

kxkpmulp  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(134)

我创建了一个名为“Customer”的表,我想创建一个简单的Yii应用程序来创建和查看。下面是我在CustomerController.php中的代码片段:

class CustomerController extends Controller
{

    public $layout = '//layouts/column2';

    public function actionCreate()
    {

        $model = new Customer();

        $this->redirect(array('view', 'id' => $model->id));

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionView()
    {

        $model = new Customer();

        $result = $model->viewCustomer();

        foreach ($result as $row) {

            echo $row["title"];
            echo $row["fname"];
            echo $row["lname"];
            echo $row["addressline"];
            echo $row["town"];
            echo $row["zipcode"];
            echo $row["phone"];
        }

        $this->render('view', array(
            'model' => $this->$model(),
        ));
    }
}

这是我的模型中名为Customer.php的代码

public function createCustomer()
{
    $connection = Yii::app()->db;
    $sql = "INSERT INTO Customer (title,fname,lname,addressline,town,zipcode,phone)VALUES(:title,:fname,:lname,:addressline,:town,:zipcode,:phone)";
    $command = $connection->createCommand($sql);
    $command->bindParam(":title", $title, PDO::PARAM_STR);
    $command->bindParam(":fname", $fname, PDO::PARAM_STR);
    $command->bindParam(":lname", $lname, PDO::PARAM_STR);
    $command->bindParam(":addressline", $addressline, PDO::PARAM_STR);
    $command->bindParam(":town", $town, PDO::PARAM_STR);
    $command->bindParam(":zipcode", $zipcode, PDO::PARAM_STR);
    $command->bindParam(":phone", $phone, PDO::PARAM_STR);
    $result = $command->execute();

    if ($result == 1) {
        return "ok";
    }

    public function viewCustomer()
    {
        $connection = Yii::app()->db;
        $sql = "Select * from Customer";
        $dataReader = $connection->createCommand($sql)->query();
        $dataReader->bindColumn(1, $title);
        $dataReader->bindColumn(2, $fname);
        $dataReader->bindColumn(3, $lname);
        $dataReader->bindColumn(4, $addressline);
        $dataReader->bindColumn(5, $town);
        $dataReader->bindColumn(6, $zipcode);
        $dataReader->bindColumn(7, $phone);
        $result = $dataReader->queryAll();
        return $result;
    }

}

但是,我总是犯着这种错误:
CDbException CDbCommand无法执行SQL语句:CDbCommand无法准备SQL语句:SQL状态[HY000]:一般错误:1.无此表:客户。执行的SQL语句为:从客户中选择 *。
我朋友说我没有get值,我该怎么解决这个问题呢?请大家帮帮我,先谢谢了。顺便说一句,我用的是PDO。

6jjcrrmo

6jjcrrmo1#

我认为你是新的编程有缺乏oops的概念。首先尝试了解oops的概念,请比你将能够理解Yii容易。
现在我不能测试你的代码,但可以提供你的建议。
您的控制器::

class CustomerController extends Controller
{

     public $layout='//layouts/column2';

     public function actionCreate()
     {

        $model = new Customer();

        $model = $model->find();

        if($model->id)
           $this->redirect(array('view','id'=>$model->id));

        $this->render('create',array(
           'model'=>$model,
       ));

    }

    public function actionView()
    {

     $model = new Customer();

     $result = $model->viewCustomer();    

     foreach ($result as $row) {

        echo $row["title"];
        echo $row["fname"];
        echo $row["lname"];
        echo $row["addressline"];
        echo $row["town"];
        echo $row["zipcode"];
        echo $row["phone"];
     }

     $this->render('view',array(
        'model'=>$model,
    ));
  }
}.

您的型号:

public function createCustomer()
{

     $connection = Yii::app()->db;

     $sql = "INSERT INTO Customer (title,fname,lname,addressline,town,zipcode,phone)VALUES(:title,:fname,:lname,:addressline,:town,:zipcode,:phone)";

    $command = $connection->createCommand($sql);

    $command->bindParam(":title",$this->title,PDO::PARAM_STR);

    $command->bindParam(":fname",$this->fname,PDO::PARAM_STR);

    $command->bindParam(":lname",$this->lname,PDO::PARAM_STR);

    $command->bindParam(":addressline",$this->addressline,PDO::PARAM_STR);

    $command->bindParam(":town",$this->town,PDO::PARAM_STR);

    $command->bindParam(":zipcode",$this->zipcode,PDO::PARAM_STR);

    $command->bindParam(":phone",$this->phone,PDO::PARAM_STR);

    $result =  $command->execute();

    if ($result == 1){

        return "ok";

    }
}

公共函数viewCustomer(){

$connection = Yii::app()->db;

    $sql = "Select * from Customer";

    $dataReader = $connection->createCommand($sql)->query();

    $dataReader->bindColumn(1,$this->title);

    $dataReader->bindColumn(2,$this->fname);

    $dataReader->bindColumn(3,$this->lname);

    $dataReader->bindColumn(4,$this->addressline);

    $dataReader->bindColumn(5,$this->town);

    $dataReader->bindColumn(6,$this->zipcode);

    $dataReader->bindColumn(7,$this->phone);

    $result = $dataReader->queryAll();

    return $result;

  }

我希望我在你的代码中所做的修改能起作用。如果你尝试使用CDbCriteria来获取你的记录,并使用CActiveRecord的保存()函数将记录保存到你的数据库中,那将是最好的。

相关问题