doumiao0498 2016-12-06 07:57
浏览 48

如何在没有createCommand的情况下从数据库中检索信息?

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?

  • 写回答

1条回答 默认 最新

  • dongshi3361 2016-12-06 08:11
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000