Help us understand the problem. What is going on with this article?

【ECCUBE3】DoctrineのFilter

More than 3 years have passed since last update.

Filterを使って強制的にWHERE句を追加する。
ここでは del_flg = 0

使用例 SoftDeleteFilter

SQLFilter の定義

/src/Eccube/Doctrine/Filter/SoftDeleteFilter.php
<?php
namespace Eccube\Doctrine\Filter;

use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Mapping\ClassMetadata;

class SoftDeleteFilter extends SQLFilter
{
    public $excludes = array();

    public function setExcludes($excludes)
    {
        $this->excludes = $excludes;

        return $this;
    }

    public function getExcludes()
    {
        return $this->excludes;
    }

    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        if ($targetEntity->hasField('del_flg') && !in_array($targetEntity->getName(), $this->getExcludes())) {
            return $targetTableAlias . '.del_flg = 0';
        } else {
            return "";
        }
    }
}

CartService

/src/Eccube/Service/CartService.php
<?php
//////// 省略 ////////
    /**
     * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。
     */
    protected function loadProductClassFromCart()
    {
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
        $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete');
        $excludes = $softDeleteFilter->getExcludes();

    // ここからProductClassエンティティのクエリに強制的にdel_flg = 0 を追加する。
        $softDeleteFilter->setExcludes(array(
            'Eccube\Entity\ProductClass',
        ));

        foreach ($this->cart->getCartItems() as $CartItem) {
            $this->loadProductClassFromCartItem($CartItem);
        }
    // del_flg = 0 追加分を削除( 元に戻す作業 )
        $softDeleteFilter->setExcludes($excludes);
    }

参考

オブジェクト取得過程

参考.php
<?php
$this->entityManager->getFilters()->getFilter('soft_delete');


get_class($this->entityManager) // "Doctrine\ORM\EntityManager"
get_class($this->entityManager->getFilters()); // "Doctrine\ORM\Query\FilterCollection"

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした