php Doctrine和X()中的动态参数

eqfvzcg8  于 2023-04-10  发布在  PHP
关注(0)|答案(2)|浏览(177)

我正在寻找一个解决方案,根据传递的参数使教义DQL语句的andX部分动态

private function getDiscountPrice(int $weighting, Article $article, Customer $customer, array $args) {

// Get the query builder
$qb = Shopware()->Models()->createQueryBuilder();
$params = new ArrayCollection();

foreach($args as $key => $arg):
    $params->set($key,$arg);
endforeach;

$qb->select('discount')
    ->from('PhaBase\Models\Discount','discount')
    ->where(
        $qb->expr()->andX(
                $qb->expr()->eq('discount.kdNr',':kdNr'),
                $qb->expr()->eq('discount.pzn',':pzn'),
                $qb->expr()->isNotNull('discount.kdNr'),
                $qb->expr()->isNotNull('discount.pzn')
        )
    )
    ->setParameters($params->toArray());

$discount = null;

try {
    $discount = $qb->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
    // @TODO Add log entry to inform about Exception
    return null;
}

我想以与Parameters相同的方式为andX()参数构建内容,但我不知道如何将动态参数传递给此方法。
谢谢你的建议,迈克尔

yacmzcpb

yacmzcpb1#

以下是解决方案-刚刚找到它:

private function getDiscountPrice(int $weighting, Article $article, Customer $customer, array $args) {

// Get the query builder
$qb = Shopware()->Models()->createQueryBuilder();

$params = new ArrayCollection();
$conditions = new ArrayCollection();

foreach($args as $key => $arg):
    $params->set($key,$arg);
    $conditions->add($qb->expr()->eq('discount.'.$key,':'.$key));
endforeach;

$conditions = call_user_func_array(array($qb->expr(), 'andX'), $conditions->toArray());

$qb->select('discount')
    ->from('PhaBase\Models\Discount','discount')
    ->where($conditions)
    ->setParameters($params->toArray());

$discount = null;

try {
    $discount = $qb->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
    // @TODO Add log entry to inform about Exception
    return null;
}

// If a discount was found calculate the price for this discount and return it.
if (!is_null($discount)):
    $discountPrice = $this->calculateDiscountPrice($article, $discount, $customer);
gpnt7bae

gpnt7bae2#

可以将add()附加表达式直接转换为andX()orX()

您可以直接将表达式添加到andX表达式中,而不是将表达式添加到ArrayCollection中(并且只有在主查询中实际存在表达式时才将其添加到主查询中)。对于所需的参数也是如此。

private function getDiscountPrice(int $weighting, Article $article, Customer $customer, array $args)
{
  // Early out if there's nothing to do.
  if (empty($args)) {
    return null;
  }

  $qb = Shopware()->Models()->createQueryBuilder()
    ->select('discount')
    ->from(PhaBase\Models\Discount::class,'discount')
  ;

  $conditions = $qb->expr()->andX();

  // Instead of setting each parameter individually (see below) you could set them 
  // all at once without the need for an ArrayCollection(), like you used, because your 
  // $args array is already in the correct format.
  // $qb->setParameters($args);

  foreach($args as $key => $arg) {
    $conditions->add('discount.'.$key.' = :'.$key);
    $qb->setParameter($key, $arg);
  }

  // You could check for $conditions->count() > 0 here, but we already did that at the beginning.
  try {
    return $this->calculateDiscountPrice(
      $article,
      $qb->where($conditions)->getQuery()->getOneOrNullResult(),
      $customer
    );
  } catch (NonUniqueResultException $e) {
    // @TODO Add log entry to inform about Exception
    return null;
 }
}

有点晚了,但也许对别人有帮助。

相关问题