Cakephp 3查询或条件

时间:2016-07-28 12:02:39

标签: cakephp-3.0

伙计们如何在以下Where声明中应用OR条件?

                    $count = 0;

                    $_productCollection = Mage::getModel('catalog/category')->load($_categoryId)
                        ->getProductCollection()
                        ->addAttributeToSelect('*')
                        ->setOrder('date', 'ASC');
                    ?>

                    <div class="home-featured-product">         
                        <?php foreach ($_productCollection as $_product): ?>
                            <li class="item">

                                <div class="regular">
                                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
                                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(270, 200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
                                    </a>
                                </div>

                                <a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
                                    <?php echo $this->htmlEscape($_product->getName()) ?>
                                </a>

                                <?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
                                <?php echo $this->getPriceHtml($productObject, true) ?>
                            </li>
                        <?php
                            if($count == 1) break; // Stop after 2 items
                            $count++; 
                        ?>
                        <?php endforeach ?>
                    </div>

变成类似

的东西
$this->IbCommisions->find()->matching(
    'Users.Accounts',function ($q) {
        return $q->where(['IbCommisions.ib_id' => $this->userid, 'Users.country_id' => $this->request->data['country']]);
}

1 个答案:

答案 0 :(得分:8)

return $q->where(['OR' => [
    'IbCommisions.ib_id' => $this->userid,
    'Users.country_id' => $this->request->data['country']
]]);

或者

return $q->where(['IbCommisions.ib_id' => $this->userid])
    ->orWhere(['Users.country_id' => $this->request->data['country']]);

http://book.cakephp.org/3.0/en/orm/query-builder.html#advanced-conditions