Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php doctrine2 queryBuilder必须只返回与数组值(id)匹配的结果:0/Null和/或一个和/或多个id必须返回一个结果_Php_Symfony_Doctrine Orm_Query Builder_Dql - Fatal编程技术网

Php doctrine2 queryBuilder必须只返回与数组值(id)匹配的结果:0/Null和/或一个和/或多个id必须返回一个结果

Php doctrine2 queryBuilder必须只返回与数组值(id)匹配的结果:0/Null和/或一个和/或多个id必须返回一个结果,php,symfony,doctrine-orm,query-builder,dql,Php,Symfony,Doctrine Orm,Query Builder,Dql,我有一个名为$configurations的集合数组。此数组匹配连接到Product.php的实体Configuration.php作为manytomy。现在我有了另一个名为WorkType.php的实体,它也通过manytomy连接到Configuration.php 目标是恢复具有当前工作类型的O/Null或多个配置的产品 默认情况下,我的产品没有配置,但用户可以通过复选框选择工作类型可用的O/Null或One或Many配置 因此,我在我的ProductRepository.php中创建了这

我有一个名为
$configurations
的集合数组。此数组匹配连接到
Product.php
的实体
Configuration.php
作为
manytomy
。现在我有了另一个名为
WorkType.php
的实体,它也通过
manytomy
连接到
Configuration.php

目标是恢复具有当前工作类型的
O/Null
多个
配置的产品

默认情况下,我的产品没有配置,但用户可以通过复选框选择工作类型可用的
O/Null
One
Many
配置

因此,我在我的
ProductRepository.php
中创建了这个queryBuilder:

public function getProductByManyConfig($slug, $configurations)
  {

    $queryBuilder = $this->getEntityManager()->createQueryBuilder();

    $queryBuilder->select('p')
                 ->from('MyBundle', 'p')
                 ->join('p.workType', 'wt', 'WITH', 'wt.id = p.workType')
                 ->leftJoin('p.configurations','c');

    $queryBuilder->add('where', $queryBuilder->expr()->in('c', ':c'))
                 ->andWhere('wt.slug = :slug')
                 ->setParameters(array('slug'=> $slug, 'c'=> $configurations));

    return $queryBuilder->getQuery()
                        ->getResult();
  }
它可以工作,但问题是它会返回与我传递给查询的配置数组匹配的所有产品

例如,如果数组具有
Configuration.php
id
12,则查询将返回与Configuration.id=1Configuration.id=2Configuration.id=1和2匹配的产品

所以我要达到三个条件,我只需要一个;e-g仅适用于以下产品: 包含所有的configuration.id

我只需要返回包含
Configuration.php
id
的唯一一个产品,该id传递到当前
WorkType.php的数组中

这是我的控制器代码:

$vars = array(); //containing all values I need for recover configurations properties

$em = $this->getDoctrine()->getManager();

$var = array_values($vars);

$configurations = $this->getDoctrine()->getRepository('MyBundle:Configuration')->findByName($var);

foreach ($configurations as $conf) {
  $name = $conf->getName();
}

$slug = "the_right_database_correspondence";

$arrayProbuctConfig = $this->getDoctrine()->getRepository('MyBundle:Product')->getProductByManyConfig($slug, $configurations);

// return of the view

由于配置的选择可能会改变,我需要创建这个动态方法。我怎样才能返回好的结果呢?

最近我对Doctrine2也有同样的问题,我得到了这个答案

如果希望所有
产品
实体与所有配置链接,则需要为每个配置添加一个
where
条件

考虑到您的
$configurations
变量是一个
配置
实体数组

$queryBuilder->select('p')
             ->from('MyBundle', 'p')
             ->join('p.workType', 'wt', 'WITH', 'wt.id = p.workType')
             ->leftJoin('p.configurations','c');

$queryBuilder->where('wt.slug = :slug')->setParameter('slug', $slug);

$nbConfs = count($configurations);
  for($i = 0; $i < count($configurations); $i++){
      $queryBuilder->andWhere(":conf{$i} MEMBER OF p.configurations")->setParameter("conf{$i}", $configurations[$i]);
  }

$queryBuilder
      ->having('COUNT(c.id) =:some_count')
      ->setParameter('some_count', $nbConfs);
$queryBuilder->select('p')
->from('MyBundle','p')
->联接('p.workType','wt','WITH','wt.id=p.workType')
->leftJoin('p.configurations','c');
$queryBuilder->where('wt.slug=:slug')->setParameter('slug',$slug);
$nbConfs=计数($configurations);
对于($i=0;$iandWhere(“:conf{$i}p.configurations的成员”)->setParameter(“conf{$i},$configurations[$i]);
}
$queryBuilder
->有('COUNT(c.id)=:some_COUNT')
->setParameter('some_count',$nbConfs);
我必须使用
for
循环来创建不同的令牌(前面的字符串是
),否则Doctrine不会将它们添加到其内部参数集合中


< >编辑:查询修改以考虑没有配置的情况,并想获得<代码>产品< /代码> s,它与<代码>配置< /代码> S.< /P> < P>我最近与RealthIn2有相同的问题,并且我得到了这个答案。 如果希望所有
产品
实体与所有配置链接,则需要为每个配置添加一个
where
条件

考虑到您的
$configurations
变量是一个
配置
实体数组

$queryBuilder->select('p')
             ->from('MyBundle', 'p')
             ->join('p.workType', 'wt', 'WITH', 'wt.id = p.workType')
             ->leftJoin('p.configurations','c');

$queryBuilder->where('wt.slug = :slug')->setParameter('slug', $slug);

$nbConfs = count($configurations);
  for($i = 0; $i < count($configurations); $i++){
      $queryBuilder->andWhere(":conf{$i} MEMBER OF p.configurations")->setParameter("conf{$i}", $configurations[$i]);
  }

$queryBuilder
      ->having('COUNT(c.id) =:some_count')
      ->setParameter('some_count', $nbConfs);
$queryBuilder->select('p')
->from('MyBundle','p')
->联接('p.workType','wt','WITH','wt.id=p.workType')
->leftJoin('p.configurations','c');
$queryBuilder->where('wt.slug=:slug')->setParameter('slug',$slug);
$nbConfs=计数($configurations);
对于($i=0;$iandWhere(“:conf{$i}p.configurations的成员”)->setParameter(“conf{$i},$configurations[$i]);
}
$queryBuilder
->有('COUNT(c.id)=:some_COUNT')
->setParameter('some_count',$nbConfs);
我必须使用
for
循环来创建不同的令牌(前面的字符串是
),否则Doctrine不会将它们添加到其内部参数集合中


编辑:查询修改以考虑没有配置的情况,并想获得<代码>产品<代码> > s,它与<代码>配置< /代码> S.</P>谢谢您的回答,我现在没有我的代码,但请相信我来尝试您的建议。顺便谢谢你!如果我输入2个或多个配置,您的解决方案工作得很好,但是如果我的数组包含0/NULL或1个配置,则查询将重新运行工作类型的所有产品。所以我需要修正这种行为。@french_dev如果我很好地理解了你的评论,我编辑了我的代码,当你传递一个空数组时,我将代码限制为没有配置的产品。关于只传递一个配置的情况,您应该只将产品连接到该配置。与我之前告诉过您的一样,您的查询工作正常,现在可以处理数组为空时的条件,但如果数组仅包含一个参数时则不能。我正在搜索创建它!谢谢你的回答。@french_dev好的,现在我完全理解你试图实现的目标。很好的协作工作,谢谢你!谢谢你的回答,我现在还没有我的代码,但请相信我会尝试你的建议。顺便谢谢你!如果我输入2个或多个配置,您的解决方案工作得很好,但是如果我的数组包含0/NULL或1个配置,则查询将重新运行工作类型的所有产品。所以我需要修正这种行为。@french_dev如果我很好地理解了你的评论,我编辑了我的代码,当你传递一个空数组时,我将代码限制为没有配置的产品。关于只通过一个配置的情况,您应该只将产品连接到该配置