【问题标题】:How to fix the Error CDbException in Yii?如何修复 Yii 中的错误 CDbException?
【发布时间】:2012-09-28 02:56:47
【问题描述】:

我制作了一个名为“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 语句:SQLSTATE[HY000]:一般错误:1 没有这样的表:客户。执行的 SQL 语句是:Select * from Customer。

我的朋友说我没有获取价值。我该如何解决这个问题?请帮帮我。先感谢您。顺便说一句,我正在使用 PDO。

【问题讨论】:

  • 您确定您的表名是 Customer 还是 customer 。大写/小写

标签: php yii


【解决方案1】:

我认为您是缺乏 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 的 save() 函数将记录保存在您的数据库中,那将是最好的。

【讨论】:

    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多