I have an entity like below:
<?php
/** @Entity */
class User
{
// ...
/**
* Many Users have Many Groups.
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(name="users_groups")
*/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** @Entity */
class Group
{
// ...
/**
* Many Groups have Many Users.
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
I have related Repositories to those entities. When there is one to many relationship it is quite easy to get related object because you can easily execute something like:
/** @var RelatedEntity $relatedEntity*/
$this->findBy('relatedObject'=>$relatedEntity);
When i try to execute it on Entities showed above instead of gathering objects thorough lets say user_group table it try to find user_group column inside user table. It generates error like:
An exception occurred while executing 'SELECT t0.id AS id_1, t0.created_at AS created_at_2, t0.updated_at AS updated_at_3, t0.title AS title_4, t0.text AS text_5 FROM user t0 WHERE user_group.group_id = ?'with params ["8192ed63-5d07-4c99-8902-006a3aa986e7"]:
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "user_group"
I have no idea how it should be done properly. What have I missed?
In a case of ManyToMany, you can create your function in the repository and user WHERE MEMBER OF, here is an example
->andWhere(':activity MEMBER OF t.activities')
->setParameter('activity', $data['activity'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.