Title: Doctrine2 get populated types for ManyToMany relation?
Tags: php;doctrine-orm;zend-framework2
Question: I'm unsure how to get all hotel types that have "at least one" hotel? As the relation is ManyToMany.
So with queryBuilder, I assumed I can do something like:
$qb = $this-&gt;em-&gt;createQueryBuilder();
$qb-&gt;select('t')
-&gt;from(HotelTyp75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0class, 't', 't.id')
-&gt;join('t.hotels', 'm');
But this doesn't seem to work? I'm not sure what I must do. Here are my two entities:
HotelType entity :
use Doctrine\ORM\Mapping as ORM;
* HotelType
* @ORM\Table(name="hotel_type")
* @ORM\Entity
class HotelType
/**
* @var string
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\ManyToMany(targetEntity="Hotel", cascade={"persist"}, mappedBy="mType")
*/
private $hotels;
Hotel entity :
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
* Hotel
* @ORM\Table(name="hotel")
* @ORM\Entity(repositoryClass="Application\Repository\HotelRepository")
class Hotel
/**
* @var string
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="HotelType", inversedBy="hotels", cascade={"persist"})
* @ORM\JoinTable(name="hotel_hoteltype",
* joinColumns={@ORM\JoinColumn(name="hotel_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="hoteltype_id", referencedColumnName="id")}
* )
*/
private $mType;
public function __construct()
$this-&gt;mType = new ArrayCollection();
public function getMType()
return $this-&gt;mType;
public function addMType($mtype)
if (!$this-&gt;mType-&gt;contains($mtype) &amp;&amp; is_object($mtype)) {
$this-&gt;mType-&gt;add($mtype);
}
return $this;
public function removeMType($mtype)
if ($this-&gt;mType-&gt;contains($mtype) &amp;&amp; is_object($mtype)) {
$this-&gt;mType-&gt;removeElement($mtype);
}
return $this;
public function clearMType()
$this-&gt;mType-&gt;clear();
return $this;
Comment: Looks good to me. Is there any error? And are you sure to not forgot `->getQuery()->getResult()`? Do your Entities have a namespace?

Comment: Hmm it seems to have been a problem elsewhere, all working now (it was giving 0 results when it should have given more... now it works)

Comment: `..doesn't seem to work` is it throwing an error? Returning nothing? Do queries on other entities work?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:07/15/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><doctrine-orm><zend-framework2>', 'Title': 'Doctrine2 get populated types for ManyToMany relation?', 'ViewCount': 32, 'id': 31427803, 'user_AboutMe': '<p>Coding something...</p>\n', 'user_AccountId': 63325.0, 'user_DisplayName': 'dan2k3k4', 'user_Reputation': 1319.0, 'user_Views': 166.0, 'user_id': 187505.0}}