Answer the question
In order to leave comments, you need to log in
DQL query in symfony (many-to-many relationship)?
I have 2 entities:
user - users
stock - user shares
<?php
namespace Stocker\StockBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="Stocker\StockBundle\Entity\Repository\UserRepository")
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Userstock", inversedBy="users")
* @ORM\JoinTable(name="userstocks")
*/
private $userstocks;
public function __construct()
{
parent::__construct();
$this->userstocks=new ArrayCollection();
}
<?php
namespace Stocker\StockBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="Stocker\StockBundle\Entity\Repository\UserstockRepository")
* @ORM\Table(name="userstock")
*/
class Userstock
{
/**
* Constructor
*/
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string",length=255)
*/
protected $stockname;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="userstocks")
*/
protected $users;
SELECT us.stockname
FROM userstock us
LEFT JOIN userstocks u_s ON us.id=u_s.userstock_id AND u_s.user_id=1
WHERE u_s.user_id IS null
Answer the question
In order to leave comments, you need to log in
And how to get a list of stocks that a given user does not have?
SELECT s.stockname FROM Stocker\StockBundle\Entity\Userstock s
WHERE s.user != :user
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question