2016-12-06 1 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を使用することができますが、あなたが

を使用するためにあなたがfindAllByAttributesを(使用できるすべてのモデルを得ることができた

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

という名前CActiveRecodeモデルクラスを持っていると仮定し

ています)

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

findByAttributes()を使用できる単一のモデルを取得するには、

をご覧ください。http://www.yiiframework.com/doc/guide/1.1/en/database.ar

関連する問題