在我的Web应用程序中,我有三个模型。即商品,制造商,商品_制造商 这是我需要执行的查询
SELECT items.id,item_desc,manufacturers.id,manufacturers.name FROM items_manufacturers,items,manufacturers WHERE items_manufacturers.item_id=item.id AND items_manufacturers.manufacturer_id=manufacturers.id
模型之间的关系是
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'item' => array(self::BELONGS_TO, 'Items', 'item_id'),
'manufacturer' => array(self::BELONGS_TO, 'Manufacturers', 'manufacturer_id'),
'itemsManufacturersLocations' => array(self::HAS_MANY, 'ItemsManufacturersLocations', 'items_manufacturer_id'),
);
}
我无法使用Yii框架中的约定编写此查询。 这就是我试过的
$im=new CDbCriteria ;
$im->with= array(
'items_manufacturers.item_id'
'items_manufacturers.manufacturer_id'
'items.item_desc'
'manufacturers.manufacturer_name'
'condition'=>'items_manufacturers.manufacturer_id=manufacturers.id
items_manufacturer.item_id=items.id'
);
))->findAll();
任何团体都对此有所帮助,因为我是这个框架的新手。我对此很感兴趣。我在items_manufacturers Controller中尝试了这个。
答案 0 :(得分:0)
您使用db列分配'with'属性,而您应该执行以下操作:
$im = new CDbCriteria;
$im->with = array('items','manufacturers'); // you put 'relation names' here, not db columns
$imf = ItemsManufacturers::model()->findAll($im);
您可以在没有标准的情况下执行此操作:
$imf = ItemsManufacturers::model()->with('items','manufacturers')->findAll();