Reputation: 361
I've defined in my Symfony application a ManyToMany Relation between the Entities 'Slideshow' and 'Slide'.
Entity Slide (excerpt):
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="slide")
*/
class Slide {
/* [...] */
/**
* @ORM\ManyToMany(targetEntity="Slideshow", inversedBy="slides", cascade={"persist"})
* @ORM\JoinTable(name="slides_to_slideshows",
* joinColumns={@ORM\JoinColumn(name="slide_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="slideshow_id", referencedColumnName="id")}
* )
*/
private $slideshows;
/**
* Constructor
*/
public function __construct()
{
$this->slideArticle = new \Doctrine\Common\Collections\ArrayCollection();
$this->slideshows = new \Doctrine\Common\Collections\ArrayCollection();
}
/* [...] */
/**
* Add slideshow
*
* @param Slideshow $slideshow
*
* @return Slide
*/
public function addSlideshow(Slideshow $slideshow)
{
$this->slideshow[] = $slideshow;
return $this;
}
/**
* Get slideshows
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSlideshows()
{
return $this->slideshows;
}
/**
* Remove slideshow
*
* @param Slideshow $slideshow
*/
public function removeSlideshow(Slideshow $slideshow)
{
$this->slideshows->removeElement($slideshow);
}
}
Entity Slideshow (excerpt):
/**
* @ORM\Entity
* @ORM\Table(name="slideshow")
*/
class Slideshow {
/* [...] */
/**
* @ORM\ManyToMany(targetEntity="Slide", mappedBy="slideshows")
*/
private $slides;
/**
* Constructor
*/
public function __construct()
{
$this->slides = new \Doctrine\Common\Collections\ArrayCollection();
}
/* [...] */
/**
* Get slides
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSlides()
{
return $this->slides;
}
/**
* Add slide
*
* @param \Screensolutions\Macms\FrontBundle\Entity\Slide $slide
*
* @return Slideshow
*/
public function addSlide(\Screensolutions\Macms\FrontBundle\Entity\Slide $slide)
{
$this->slides[] = $slide;
return $this;
}
/**
* Remove slide
*
* @param \Screensolutions\Macms\FrontBundle\Entity\Slide $slide
*/
public function removeSlide(\Screensolutions\Macms\FrontBundle\Entity\Slide $slide)
{
$this->slides->removeElement($slide);
}
}
When I make a DB query, I get this error message:
An exception occurred while executing 'SELECT t0.id AS id_1, t0.deleted AS deleted_2, t0.title AS title_3, t0.link AS link_4, t0.rank AS rank_5, t0.artist_id AS artist_id_6 FROM slide t0 WHERE slides_to_slideshows.slideshow_id = ? ORDER BY t0.deleted ASC, t0.rank ASC, t0.title ASC' with params ["2"]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'slides_to_slideshows.slideshow_id' in 'where clause'
The table slides_to_slideshows exists, the field slideshow_id as well, so I guess something's wrong with my definition of the relation. I wonder why the query is slides_to_slideshows.slideshow_id instead of FROM slide t0, slides_to_slideshows t1 WHERE t1.slideshow_id?
Any hints?
best regards
EDIT: This is one query that provokes the error:
$repository = $this->getDoctrine()->getRepository('FrontBundle:Slide');
$slidesAll = $repository->findBy(array('slideshows' => $id), array('deleted' => 'ASC', 'rank' => 'ASC', 'title' => 'ASC'));
As suggested by Cerad I made a test with a simple findAll() query and that works flawless. The problem is the where clause 'slideshows' => $id, so I suppose there´s something wrong with the definition of the relation.
The cache was cleared at least a thousand times, the database was updated by doctrine:schema:update --force and doctrine:schema:validate does not report any problems with this entities.
Any suggestions? I'm thankful for every hint - this problem makes me insane :(
Upvotes: 1
Views: 489
Reputation: 1529
You should do something like this
$repository = $this->getDoctrine()->getRepository('FrontBundle:Slide')->createQueryBuilder('s')
->join('s.slideshows', 'sw')
->andWhere('sw.id=:id')->setParameter('id', $id)
->addOrderBy('s.deleted', 'ASC')
...
Upvotes: 2