How to fix the Error CDbException in Yii?

I made a table called "Customer" and I want to make a simple Yii application of create and view. Here's the piece of my code in 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(),
        ));
    }
}

and here's the code in my model named 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;
    }

}

But, I always having this kind of error:

CDbException CDbCommand failed to execute the SQL statement: CDbCommand failed to prepare the SQL statement: SQLSTATE[HY000]: General error: 1 no such table: Customer. The SQL statement executed was: Select * from Customer.

My friend said that I don't have a get value. How can I resolve this? Please help me guys. Thank you in advance. BTW, I'm using PDO.


I think you are new in the programming having lack of oops concepts. First try to understand oops concepts please than you would be able to understand Yii easily.

Right now i can't test your code but may provide you suggestion.

Your controller::

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,
    ));
  }
}.

Your 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";

    }
}

public function 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;

  }  

I hope my changes in your code will work. It would be best if you try to use CDbCriteria to fetch your records & save() function of CActiveRecord to save record in your database.

链接地址: http://www.djcxy.com/p/60846.html

上一篇: 使用Yii显示来自2个模型的数据

下一篇: 如何解决Yii中的错误CDbException?