如何获得只有产品的图像在CakePhp [英] How to get only products with images in CakePhp

查看:231
本文介绍了如何获得只有产品的图像在CakePhp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php 
class Product扩展AppModel {
var $ name ='Product';

//下面的协会已经创建了所有可能的键,那些不需要的可以删除

var $ belongsTo = array(
'Category' => array(
'className'=>'Category',
'foreignKey'=>'category_id',
'conditions'=>'',
'fields'=>'',
'order'=>''
),
'Brand'
);


var $ hasOne = array(
'PhotoSmall'=> array(
'className'=>'Photo',
'foreignKey '=>'product_id',
'dependent'=> true,
'conditions'=>'PhotoSmall.tipo =small',
'fields'=& '',
'order'=>'',
'limit'=>'',
'offset'=>'',
'exclusive' >',
'finderQuery'=>'',
'counterQuery'=>''
),
);

var $ hasMany = array(
'PhotoBig'=> array(
'className'=>'Photo',
'foreignKey'=& 'product_id',
'dependent'=> true,
'conditions'=>'PhotoBig.tipo =big',
'fields'=> b $ b'order'=>'PhotoBig.order',
'limit'=>'',
'offset'=>'',
'exclusive'=> ;'',
'finderQuery'=>'',
'counterQuery'=>''
),
'Rating'=& b'className'=>'Rating',
'foreignKey'=>'product_id',
'dependent'=> false,
'conditions'=&
'fields'=>'',
'order'=>'',
'limit'=>'',
'offset'=> ',
'exclusive'=>'',
'finderQuery'=>',
'counterQuery'=> ''

);

我需要所有的发现(管理区域除外)只检索带有图片的产品。 p>

PhotoSmall 的情况下很容易,我可以做 PhotoSmall.id IS NOT NULL 在一个条件,因为CakePhp生成一个左连接,但我找不到一个方法要求最少在 PhotoBig ,因为CakePhp做两个查询返回



要求如下,我只能显示带有 PhotoSmall 和至少一个 PhotoBig 系统范围内(除了站点的管理部分),解决方案应该与CakePhp分页一起使用



尝试以下代码没有成功,它只返回产品数据,而不是照片数据:

  $ this-> Product-> recursive = -1; 
$ conditions = array('Product.category_id'=> $ cats);
$ joins = array(
array('table'=>'photos',
'alias'=>'PhotoBig',
'type'=> INNER',
'conditions'=> array(
'Product.id = PhotoBig.product_id',

) >'photos',
'alias'=>'PhotoSmall',
'type'=>'INNER',
'conditions'=& 'Product.id = PhotoBig.product_id',


);
$ this-> paginate = array(
'limit'=> 12,
'conditions'=> $ conditions,
'joins'=> $ join ,
);


解决方案

首先你设置递归到-1



为什么你在产品模型上的查找中不使用AND条件,如下:

/ p>

  $ this-> Product-> find('all',array('recursive'=> 1,'conditions '=> array(
NOT=> array('PhotoSmall.id'=> NULL,'PhotoBig.id'=> NULL))));

注意:递归至少需要一个,因为您需要相关数据。


<?php
class Product extends AppModel {
    var $name = 'Product';

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Brand'
    );


    var $hasOne = array(
        'PhotoSmall' => array(
            'className' => 'Photo',
            'foreignKey' => 'product_id',
            'dependent' => true,
            'conditions' => 'PhotoSmall.tipo = "small"',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
    );

    var $hasMany = array(
        'PhotoBig' => array(
            'className' => 'Photo',
            'foreignKey' => 'product_id',
            'dependent' => true,
            'conditions' => 'PhotoBig.tipo = "big"',
            'fields' => '',
            'order' => 'PhotoBig.order',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Rating' => array(
            'className' => 'Rating',
            'foreignKey' => 'product_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

I need all my finds (except in the admin area) to retrieve only products with images.

In the case of PhotoSmall is easy, I can do PhotoSmall.id IS NOT NULL in a condition because CakePhp generates a left join, but i can not find a way to require a least on PhotoBig because CakePhp does two queries to return its results.

The requirement is the following, I can only show products with a PhotoSmall and a least one PhotoBig system wide (except for the admin section of the site), the solution should work with CakePhp pagination

I've tried the following code without success, it only returns product data, not photo data:

$this->Product->recursive = -1;
$conditions = array('Product.category_id'=> $cats);
$joins = array(
    array('table' => 'photos',
        'alias' => 'PhotoBig',
        'type' => 'INNER',
        'conditions' => array(
            'Product.id = PhotoBig.product_id',
        )
    ),
    array('table' => 'photos',
        'alias' => 'PhotoSmall',
        'type' => 'INNER',
        'conditions' => array(
            'Product.id = PhotoBig.product_id',
        )
    )
);
$this->paginate = array(
    'limit' => 12,
    'conditions' => $conditions,
    'joins' => $joins,
);

解决方案

Well firstly you're setting recursive to -1 which should just give you Product data no matter what kind of find you do.

Why don't you do an AND condition in a find on the Product model like so:

    $this->Product->find('all', array('recursive' => 1, 'conditions' => array(
    "NOT" => array('PhotoSmall.id' => NULL, 'PhotoBig.id' => NULL))));

Note: recursive will need to be at least one because you want related data.

这篇关于如何获得只有产品的图像在CakePhp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆