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 与条令的许多关系';s QueryBuilder和Symfony2_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php 与条令的许多关系';s QueryBuilder和Symfony2

Php 与条令的许多关系';s QueryBuilder和Symfony2,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,在我工作的网站上,我需要根据用户的设置将结果限制在相当多的地方。这是名为Countries的用户上的一个成员,它本身就是一个实体。我把它设置成这样: /** * @ORM\Entity * @ORM\Table(name="county") */ class County { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") *

在我工作的网站上,我需要根据用户的设置将结果限制在相当多的地方。这是名为Countries的用户上的一个成员,它本身就是一个实体。我把它设置成这样:

/**
 * @ORM\Entity
 * @ORM\Table(name="county")
 */
class County
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    protected $name;

    // ... Getters and Setters
}
// ...

/**
 * @ORM\ManyToMany(targetEntity="App\Bundle\UserBundle\Entity\County")
 * @ORM\JoinTable(name="user_county_xref",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="county_id", referencedColumnName="id")}
 * )
 */
protected $counties;

// ...

/**
 * Add counties
 *
 * @param \App\Bundle\UserBundle\Entity\County $counties
 * @return User
 */
public function addCountie(\App\Bundle\UserBundle\Entity\County $counties)
{
    $this->counties[] = $counties;

    return $this;
}

/**
 * Remove counties
 *
 * @param \App\Bundle\UserBundle\Entity\County $counties
 */
public function removeCountie(\App\Bundle\UserBundle\Entity\County $counties)
{
    $this->counties->removeElement($counties);
}

/**
 * Get counties
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getCounties()
{
    return $this->counties;
}
在我的用户实体上,我将其连接如下:

/**
 * @ORM\Entity
 * @ORM\Table(name="county")
 */
class County
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string")
     */
    protected $name;

    // ... Getters and Setters
}
// ...

/**
 * @ORM\ManyToMany(targetEntity="App\Bundle\UserBundle\Entity\County")
 * @ORM\JoinTable(name="user_county_xref",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="county_id", referencedColumnName="id")}
 * )
 */
protected $counties;

// ...

/**
 * Add counties
 *
 * @param \App\Bundle\UserBundle\Entity\County $counties
 * @return User
 */
public function addCountie(\App\Bundle\UserBundle\Entity\County $counties)
{
    $this->counties[] = $counties;

    return $this;
}

/**
 * Remove counties
 *
 * @param \App\Bundle\UserBundle\Entity\County $counties
 */
public function removeCountie(\App\Bundle\UserBundle\Entity\County $counties)
{
    $this->counties->removeElement($counties);
}

/**
 * Get counties
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getCounties()
{
    return $this->counties;
}
这一切都像它应该的那样工作,我可以毫无问题地将县附加到用户

然后我有第二个实体,叫做Store,它也有这些县实体。我做了相同的声明,但将
JoinTable
名称更改为不同的名称,并将
jointColumn
更改为使用
store\u id
而不是
user\u id
。就其本身而言,这一切都很好。我可以将县附加到商店

现在,我想做的是根据用户的县限制县。例如,如果用户在A县和B县,我只想看到A县和B县的商店

我不知道如何使用DQL或查询生成器来实现这一点。当我试图在表单中使用QueryBuilder时,这会使情况变得更糟,因为我无法获得正确的QueryBuilder语法,所以我无法找到一种方法来限制数据本身

当以这种方式设置实体时,如何执行QueryBuilder或DQL语句

编辑

以下是我尝试复制的SQL语句,以供参考:

SELECT S.* 
FROM store S 
JOIN store_county_xref SCX 
    ON SCX.store_id=S.id 
JOIN user_county_xref UCX 
    ON UCX.county_id=RCX.county_id 
WHERE UCX.user_id = 1 
问题的一部分是我不确定应该使用什么。如果我添加mappedBy或inversedBy,Doctrine会抱怨索引不存在(因为它似乎在查看实体表,而不是声明的JoinTable)。我无法加入这样的表,因为条令抱怨这两个实体之间没有关联:

SELECT s 
FROM AppStoreBundle:Store s 
JOIN AppUserBundle:County c WITH c.store = s.id
这是有道理的,因为县上没有“商店”成员,因为商店和用户应该有县,而不是相反,因为县只是一种标签

我甚至尝试过这个,认为它会起作用,但没有多大希望:

SELECT s 
FROM AppStoreBundle:Store s 
JOIN AppUserBundle:User u WITH u.counties = s.counties

我会改用直接SQL而不是DQL/QueryBuilder,但我不知道如何在表单内部运行直接SQL,这是一个主要缺点

您可以在

我已经排除了下面代码示例中的其他注释

class County
{
    /**
     * @ManyToMany(targetEntity="User", mappedBy="counties")
     **/
    protected $users;
 }

 class User
 {
      /**
       * @ORM\ManyToMany(targetEntity="App\Bundle\UserBundle\Entity\County", inversedBy="users")
       * @ORM\JoinTable(name="user_county_xref",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="county_id", referencedColumnName="id")}
       * )
       */
      protected $counties;
 }
这将是正确的注释,并在实体存储库中进行正确的查询

 $queryBuilder = $this->createQueryBuilder('c');

 $queryBuilder->select(array('c', 'u'))
              ->leftJoin('c.users', 'u');

 return $queryBuilder->getQuery()->getResult();
对于其他实体,它应该完全相同。

谢谢。好榜样!真的帮助了我。请参见下面我的示例queryBuilder。其中
string$search、int$locale

$q  = $qb->select(array('a'))
                ->from(Article::class, 'a')
                ->leftJoin('a.locales', 'l')
                ->where(
                    $qb->expr()->like('a.articleName',"'%$search%'")
                )
                ->andWhere('l.id = '.$locale)
                ->orderBy('a.id', 'DESC')
                ->getQuery();
            $articles= $q->getResult();

            if ($articles==null)
                throw new \Exception('Article with key '.$search.' not found');

文章
知道
区域设置
中的
区域设置
。我在我的项目中也有很多关系:article->article\u has\u locale您应该在您的实体中使用mappedBy和inversedBy选项,这样就可以有一个拥有方。另外,您是否尝试过任何DQL语法?你能分享一些吗?用我试图复制的SQL以及我尝试过的一些示例DQL更新了这个问题。如果我理解正确,County实体将需要为引用它的每个对象都有一个成员?因此,如果我以后添加一个市场实体,其中有一个县与之关联,我将需要向该县实体添加一个“市场”成员?是的,这就是条令如何确定加入实体之间的关联并使之水合。另外,
mappedBy
inversedBy
的值是关系另一侧的成员名称。