如何在 CakePhp 中仅获取带有图像的产品

发布于 2024-12-26 12:28:14 字数 2909 浏览 2 评论 0原文

<?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' => ''
        )
    );

我需要我的所有发现(管理区域除外)来仅检索带有图像的产品。

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',
        )
    ),
    array('table' => 'photos',
        'alias' => 'PhotoSmall',
        'type' => 'INNER',
        'conditions' => array(
            'Product.id = PhotoBig.product_id',
        )
    )
);
$this->paginate = array(
    'limit' => 12,
    'conditions' => $conditions,
    'joins' => $joins,
);
<?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,
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2025-01-02 12:28:15

首先,您将 recursive 设置为 -1,无论您进行哪种查找,它都应该只为您提供产品数据。

为什么不在 Product 模型的查找中执行 AND 条件,如下所示:

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

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

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.

始于初秋 2025-01-02 12:28:14

你需要额外使用包含 - 至少对我有用
(由于您的递归=-1):

'contain' => array('PhotoSmall', 'PhotoBig'),

连接只是设置配置。如果不指定您的数据,您将只能获得一个模型的数据。

你使用可遏制行为吗?将是最简单的解决方案。
如果不是,请尝试将递归设置为 0 或 1。

PS:您的第二个别名在“条件”内是错误的(应该是 PhotoSmall )。

you need to additionally use contain - at least that worked for me
(due to your recursive=-1):

'contain' => array('PhotoSmall', 'PhotoBig'),

the joins are only the setup configuration. without specifying your data you will only get the one model's data.

do you use the containable behavior? would be the easiest solution.
if not, try to set recursive to 0 or 1.

PS: and your second alias is wrong inside the 'conditions' (should be PhotoSmall there).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文