Yii SQL到CActiveDataProvider

I need help from Yii experts, I have a SQL query

SELECT p.name , p.price FROM products as p, prod_cat as pc WHERE pc.id_pr  =  p.id_pr AND pc.id_cat in (SELECT id_cat FROM prod_cat where id_pr=<myid>)

I need to convert this to CActiveDataProvider in Yii. something like this

            $dataProvider2=new CActiveDataProvider('Products',array(
                        'criteria' => array(
                             ...                                      
                        ),

                ));

I have 3 Tables : Products, Categories and Prod_cat (connection between two others). I need to find others products from the same category of item that i provide ().

2个回答



我会在您的数据库中创建一个视图(使用该查询)。 然后根据该视图在Yii中创建一个模型。 创建一个activedataprovider将很容易。 </ p>

http://en.wikipedia.org/wiki/ View_(数据库)</ p>
</ div>

展开原文

原文

I would create a view in your database (using that query). Then make a model in Yii based on that view. Creating an activedataprovider will then be easy.

http://en.wikipedia.org/wiki/View_(database)

doulan2827
doulan2827 视图会慢你查询速度有时候。
接近 5 年之前 回复
dsafyx8454
dsafyx8454 我发现phpmyadmin缺乏通过GUI添加视图,但是如果你使用直接sql如create view view_name作为select等,它就可以正常工作。 在GII中制作模型是直截了当的,在输入前几个字符后出现了视图名称。
6 年多之前 回复



一个想法是使用带关系的模型。
http://www.yiiframework.com/doc/guide/1.1/en/database.arr </ p>

另一种方法是使用DAO,但它不适用于CActiveDataProvider </ p>
</ div>

展开原文

原文

One Idea is to use an model with relations. http://www.yiiframework.com/doc/guide/1.1/en/database.arr

The other way is to use DAO, but it does not work with CActiveDataProvider

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐