2

I have following code in my controller:

$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 is responsible for retrieving types of the product. (e.g if the name of the product is HP and type($gettype) is PC, it will display the HP product where type is PC). I could not realize this function without createCommand. How can I do it?

Sergej
  • 1,082
  • 11
  • 27
phpdev
  • 511
  • 4
  • 22

1 Answers1

1

You could use CActiveRecord features

assuming you have a CActiveRecode model class named

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

you could use

For obtain all the models you can use findAllByAttributes()

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

for obtain a single model you can use findByAttributes()

you can take a look at http://www.yiiframework.com/doc/guide/1.1/en/database.ar

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107