Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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 条令-多个筛选结果_Php_Symfony_Doctrine Orm_Doctrine - Fatal编程技术网

Php 条令-多个筛选结果

Php 条令-多个筛选结果,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我需要从条令中的多对多关系中得到一个筛选结果 Class Users extends RecordItem { /** * @Id @Column(type="integer") @GeneratedValue * @var int **/ protected $id; /** * @ManyToMany(targetEntity="Company") * @JoinTable(name="users_join_compan

我需要从条令中的多对多关系中得到一个筛选结果

Class Users extends RecordItem {
    /**
    * @Id @Column(type="integer") @GeneratedValue
    * @var int
    **/

    protected $id;
     /**
     * @ManyToMany(targetEntity="Company")
     * @JoinTable(name="users_join_company",
     *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="company_id", referencedColumnName="id")}
     *      )
     */
    protected $companys;

    /**
    * @Column(type="string", length=100)
    * @var string
    */
    protected $username;   
    //edit - > added array collection - forgotten
    public function __construct() {
        $this->companys = new ArrayCollection();
    }
}

Class Company extends Recorditem {

    /**
    * @Id @Column(type="integer") @GeneratedValue
    * @var int
    **/
    protected $id;

    /**
    * @Column(type="string", length=100)
    * @var string
    */
    protected $company_name;  
}
到目前为止,我只能从以下代码中查询所有公司,有没有合适的方法添加过滤器?示例:数组集合中有3个公司,希望返回一个指定公司“id”的公司


使用查询生成器,您可以使用以下内容:

$s_program = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult();
在您的情况下,请尝试以下替代品:

$user = $entityManager->getRepository('Users')->setMaxResults(1)->getOneOrNullResult();

我不知道它是否管用,但试试看

你的问题是,公司是懒散的,不受任何限制的影响
$user->getcompanys()
将始终返回所有公司

您需要a)使用QueryBuilder,b)从过滤结果集中删除实体

例如

这将获取id为1的所有用户(因此只有一个用户),并且只获取id与1匹配的公司


如果需要,可以使用getOneOrNullResult()而不是getResult来获取单个用户。

如果要筛选集合,请使用ArrayCollection

例如:

// I guess companies is an ArrayCollection
$myDesiredCompanies = $companies->filter(function (Company $entry) {
    return ($entry->getId() == 1);
});

它将过滤集合并返回一个新集合,其中包含您所需的测试结果

并返回错误“方法名称必须以findBy或findOneBy开头”,但有点理解其意图。将尝试解决此问题。是的,您是对的。这是一个ArrayCollection在尝试简化代码时意外删除了该构造。非常感谢。
$qb = $this->getEntityManager()->createQueryBuilder();
$users = $qb->select('u', 'c') // important - select all entities in query to avoid lazy loading which ignores query constraints
  ->from('YourBundle:Users', 'u')
  ->join('u.companys', 'c')
  ->where('u.id = :userId')
  ->andWhere('c.id = :companyId')
  ->setParameter('userId', 1)
  ->setParameter('companyId', 1)
  ->getQuery()
  ->getResult();
// I guess companies is an ArrayCollection
$myDesiredCompanies = $companies->filter(function (Company $entry) {
    return ($entry->getId() == 1);
});