2016-12-06 85 views
2

我已经在我的控制器下面的代码:如何在没有createCommand的情况下从数据库检索信息?

$listproduct=Yii::app()->db->createCommand() 
    ->select('product') 
    ->from('product_form') 
    ->where('product_name=:product_name and type=:type', array(':product_name'=>'HP', ':type'=>$gettype)) 
    ->queryRow(); 

$gettype负责检索类型的产品。 (例如,如果产品的名称是HP,并且type($gettype)是PC,则它将显示类型为PC的HP产品)。没有createCommand,我无法实现此功能。我该怎么做?

回答

1

你可以使用的CActiveRecord功能

假设你有一个名为

class ProductForm extends CActiveRecord 
{ 
    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
    ....... 

一个CActiveRecode模型类,你可以使用

为了获得所有可以使用findAllByAttributes()

$listProduct= ProductForm::model()-> 
     findAllByAttributes(array('product_name'=>'HP', 'type' =>$gettype)); 
模型

获取单个模型可以使用findByAttributes()

你可以看看http://www.yiiframework.com/doc/guide/1.1/en/database.ar

相关问题