Kapitel 8.4 – eigener Condition Handler

Hallo,

versuche grade mittels eigener Condition Artikel zurück zu bekommen, die nicht aktiv sind. Leider klappt das nicht, da scheinbar in der Standard-Query schon auf active = 1 abgefragt wird. Wenn ich jetzt mit meiner Condition die Query erweitere um

...
$query->where('active = :active');
$query->setParameter(':active', 1);
...

erhalte ich die Fehlermeldung _“Integrity constraint violation: 1052 Column ‘active’ in where clause is ambiguous’”. _Kann ich in dem Fall irgendwie die Abfrage auf active aus der Standardabfrage neutralisieren, damit meine Condition greift und alle Artikel auswirft, die nicht aktiv sind?

Danke und Gruss,
Oliver

muss das nicht so lauten? : 

$query->setParameter('active',1);

EDIT: ggf auch so:

$query->setParameter('active',true);

ps: 0 = nicht aktiv, 1 = aktiv, true = aktiv ?

ich will ja in dem Fall alle Artikel ausgeben, die nicht aktiv sind, als active = false bzw. 0.

Das scheint sich dann mit der Standard-Query zu beißen, weil dort scheinbar schon ein active = 1 in der where-clause integriert ist, denke ich. Es müsste also eine Möglichkeit geben, die Condition aus der Standard-Query erst mal zu entfernen. Aber wie? 

  1. :active vs active
  2. hast du mehrere Tabellen in der Query? ggf brauchst du ->where(‚richtigetabelle.active = :active‘)

hab ich auch schon versucht ->where(‚s_articles.active = :active‘)

Wenn ich das ganze z.B. auf 

$query->where(‚name = :name‘)
$query->setParameter(’:name’, ‚irgendeinartikelname‘)

setze kommt der abgefragte Artikel im Frontend raus. Wie gesagt, nur mit dem active gehts nicht, da wie ich vermute  in $query dann sowas entsteht WHERE active = 1 AND active = 0, was sich natürlich gegenseitig ausschließt und dann in einem „Integrity constraint violation: 1052 Column ‚active‘ in where clause is ambiguous’“ endet

poste bitte deine komplette query

 

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:

? Das Problem steht doch direkt vor der Nase?

sorry, aber im Moment nicht vor meiner :wink: