public function product($media_ad_id = 0, $product_id = 0)
{
$this->MediaAd->id = $media_ad_id;
$media_ad = $this->MediaAd->read();
if (!$media_ad) {
$this->redirect(array(
'action' => 'nofound'
));
}
//这段代码用于验证用户操作
$this->loadModel('MerchantProduct');
$this->loadModel('MerchantProductCategory');
$this->loadModel('MerchantProductPicture');
//载入模型
$this->MerchantProduct->recursive = -1;
//关闭关联关系
$product = $this->MerchantProduct->find('first', array(
'fields' => array(
'MerchantProduct.id',
'MerchantProduct.details',
'MerchantProduct.product_name',
'MerchantProduct.sell_price',
'MerchantProductCategory.id',
'MerchantProductCategory.name'
),
// 需要的字段
'joins' => array(
array(
'table' => 'merchants',
'alias' => 'Merchant',
'type' => 'inner',
'conditions' => array(
'Merchant.id = MerchantProduct.merchant_id'
)
),
//关联
array(
'table' => 'merchant_product_categories',
'alias' => 'MerchantProductCategory',
'type' => 'inner',
'conditions' => array(
'MerchantProductCategory.id = MerchantProduct.cate_id'
)
)
),
'conditions' => array(
'MerchantProduct.id' => $product_id,
'MerchantProduct.active=1',
'Merchant.user_id' => $media_ad['MediaAd']['user_id']
)
//条件
));
if (!$product) {
$this->redirect(array(
'action' => 'nofound'
));
}
// 图片
$pic = $this->MerchantProductPicture->find('list', array(
'conditions' => array(
'MerchantProductPicture.product_id' => $product_id
)
));
//单独获取这部分数据也是可以的哈、
$this->set(compact('product', 'pic', 'media_ad_id', 'media_ad'));
//输出到页面
}