I have a problem in symfony2 and doctrine2. When I add a Criteria exception, it doesn't appear like I want. This is the code:
Model
/**
* @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 $friendsof;
public function getusersre() {
$criteria = new Criteria();
$criteria->Where($criteria->expr()->eq('accepted', '1'));
return $this->friendsof->matching($criteria);
}
Output
SELECT te.id AS id, te.email AS email, te.username AS username
FROM users te JOIN friends t ON t.user_id = te.id
WHERE t.friend_id = ? AND te.accepted = ?
I want that it appear like this:
WHERE t.friend_id = ? AND t.accepted = ?
Doctrine generates intermediate table for Many to Many
relationship. And you can't add any field to that table. I mean field accepted
. Also you have three tables:
In your getusersre
function you want to try get friends. As I understood it is impossible to add accepted
field for your criteria as you wanted.
Also I have changed your structure. What do you think about it?
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @ORM\OneToMany(targetEntity="Friend", mappedBy="userId")
*/
private $friends;
//Other fields, getters and setters.
}
class Friend
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $userId;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
* @ORM\JoinColumn(name="friend_id", referencedColumnName="id")
*/
protected $friendId;
/**
* @ORM\Column(name="accepted", type="boolean")
*/
protected $accepted;
//Other fields, getters and setters
}
The name of Friend
entity is not solid. You can use FriendRelationship
.
Also,function getAcceptedFriends
will like that.
public function getAcceptedFriends()
{
$cr = Criteria::create();
$cr->andWhere($cr->expr()->eq('accepted', 1));
return $this->friends->matching($cr);
}
Or
public function getAcceptedFriends()
{
$friends = [];
/** @var Friend $friend */
foreach ($this->friends as $friend) {
if ($friend->getAccepted()) $friends[] = $friend;
}
return $friends;
}
You can use like that.
foreach($user->getAcceptedFriends() in $friendRelationship){
// $friendRelationship is a instance of Friend.
$username = $friendRelationship->getFriendId()->getUsername();
}
In twig
{% for fr in user.acceptedFriends %}
{{ fr.friendId.username }}
{% else %}
{{ 'No friend' }}
{% endfor %}
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.