Kapitel 8.4 – eigener Condition Handler

 

Anbei das gleiche was funktioniert und z.B. alle Artikel mit Namen Loremipsum ausgibt:

namespace MyTestPlugin\Components\SearchBundleDBAL\Condition;

use Shopware\Bundle\SearchBundle\ConditionInterface;
use Shopware\Bundle\SearchBundleDBAL\ConditionHandlerInterface;
use Shopware\Bundle\SearchBundleDBAL\QueryBuilder;
use Shopware\Bundle\StoreFrontBundle\Struct;
use Shopware\Bundle\StoreFrontBundle\Struct\ShopContextInterface;

class MyActiveConditionHandler implements ConditionHandlerInterface
{
    const ACTIVE_INCLUDED = 'my_active_included';

    /**
     * @inheritdoc
     */
    public function supportsCondition(ConditionInterface $condition)
    {
        return ($condition instanceof MyActiveCondition);
    }

    /**
     * @inheritdoc
     */
    public function generateCondition(
        ConditionInterface $condition,
        QueryBuilder $query,
        ShopContextInterface $context
    ) {

        if (!$query->hasState(self::ACTIVE_INCLUDED)) {
            $query->where('name = :name');
            $query->setParameter(':name', 'loremipsum');
            $query->addState(self::ACTIVE_INCLUDED);
        }
    }
}

 

Anbei der ConditionHandler, der den SQL-Fehler schmeisst:

namespace MyTestPlugin\Components\SearchBundleDBAL\Condition;

use Shopware\Bundle\SearchBundle\ConditionInterface;
use Shopware\Bundle\SearchBundleDBAL\ConditionHandlerInterface;
use Shopware\Bundle\SearchBundleDBAL\QueryBuilder;
use Shopware\Bundle\StoreFrontBundle\Struct;
use Shopware\Bundle\StoreFrontBundle\Struct\ShopContextInterface;

class MyActiveConditionHandler implements ConditionHandlerInterface
{
    const ACTIVE_INCLUDED = 'my_active_included';

    /**
     * @inheritdoc
     */
    public function supportsCondition(ConditionInterface $condition)
    {
        return ($condition instanceof MyActiveCondition);
    }

    /**
     * @inheritdoc
     */
    public function generateCondition(
        ConditionInterface $condition,
        QueryBuilder $query,
        ShopContextInterface $context
    ) {

        if (!$query->hasState(self::ACTIVE_INCLUDED)) {
            $query->where('active = :active');
            $query->setParameter(':active', 1);
            $query->addState(self::ACTIVE_INCLUDED);
        }
    }
}

Also eigener Condition-Handler funktioniert offensichtlich, nur halt nicht auf die benötigte Spalte active :frowning: