【问题标题】:how to set more than one condition in Doctrine ManyToMany如何在 Doctrine ManyToMany 中设置多个条件
【发布时间】:2015-11-18 21:17:11
【问题描述】:

如何在多对多原则中设置多个条件..

型号

/**
 * @ORM\ManyToMany(targetEntity="users",inversedBy="friends",cascade={"ALL"})
 * @ORM\JoinTable(name="friends",
 *      joinColumns={@ORM\JoinColumn(name="friend_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
 *      )
 */
private $groupsa;

public function getusersre() {
    return $this->groupsa;
}

输出

    SELECT 
  t0.id AS id_1, 
  t0.email AS email_2, 
  t0.username AS username_3, 
  t0.password AS password_4, 
  t0.first_name AS first_name_5, 
  t0.last_name AS last_name_6, 
  t0.location AS location_7, 
  t0.remember_token AS remember_token_8, 
  t0.created_at AS created_at_9, 
  t0.updated_at AS updated_at_10 
FROM 
  users t0 
  INNER JOIN friends ON t0.id = friends.user_id 
WHERE 
  friends.friend_id = ?

我希望它是这样的

 WHERE 
  friends.friend_id = ?
or friends.xxxx_id = ?

等待回复,谢谢大家......

【问题讨论】:

  • friends.xxxx_id 是什么意思?有什么特别的领域吗?如果您同时发布 UsersFriends 实体可能会有所帮助。
  • 请向我们展示您目前拥有的查询构建器或 DQL 代码。
  • 看看你以前的问题,我认为你应该阅读how do I ask a good question?

标签: symfony doctrine-orm doctrine


【解决方案1】:

您可以按照@SergioIvanuzzo 的建议进行操作,但如果您想在您的实体中执行此操作,您也可以使用Doctrine\Common\Collections\Criteria 类的教义。您可以在文档章节 8.8. Filtering Collections 中阅读所有相关信息。

public function getFriends(){
    $id = 1; // ...the id you want to use for filtering
    $criteria = Criteria::create()
        ->where(Criteria::expr()->eq("xxxx_id", $id));

    return $this->friends->matching($criteria);
}

【讨论】:

  • @hz4web 使用 Criteria 是否成功?这就是您要找的东西吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多