Instead of querying for blog use tag.
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeBundle:Tag')
->findOneByTag($tag);
$blogs = $tags->getBlogs();
return array(
'blogs' => $blogs,
);
Вопрос
I'm trying to setup selectable Tags to display/bring up Blogs that are linked to the tags.
What am I doing wrong? Is the problem with the doctrine query or how it's been setup on the entity side?
I'm getting the following error:
ContextErrorException: Notice: Undefined index: joinColumns in /.../vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1665
This is the current query I am using:
public function tagAction($tag)
{
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeBundle:Blog')
->findByTags($tag);
$blogs = $tags->getBlogs();
return array(
'blogs' => $blogs,
);
}
And my entity setup: (Setup as ManyToMany/ManyToMany)
Tag:
/**
* @ORM\ManyToMany(targetEntity="Blog", mappedBy="tags")
*/
protected $blogs;
public function __construct()
{
$this->blogs = new ArrayCollection();
}
/**
* Add blogs
*
* @param \AcmeBundle\Entity\Blog $blogs
* @return Tag
*/
public function addBlog(\AcmeBundle\Entity\Blog $blogs)
{
$this->blogs[] = $blogs;
return $this;
}
/**
* Remove blogs
*
* @param \AcmeBundle\Entity\Blog $blogs
*/
public function removeBlog(\AcmeBundle\Entity\Blog $blogs)
{
$this->blogs->removeElement($blogs);
}
/**
* Get blogs
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBlogs()
{
return $this->blogs;
}
Blog:
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="blogs")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* Add tags
*
* @param \AcmeBundle\Entity\Tag $tags
* @return Blog
*/
public function addTag(\AcmeBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* @param \AcmeBundle\Entity\Tag $tags
*/
public function removeTag(\AcmeBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
Решение
Instead of querying for blog use tag.
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeBundle:Tag')
->findOneByTag($tag);
$blogs = $tags->getBlogs();
return array(
'blogs' => $blogs,
);
Другие советы
I think this error happens because you have the wrong annotation for ManyToMany associations in:
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="blogs")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
protected $tags;
For ManyToMany associations I use code as below:
/**
* @ManyToMany(targetEntity="Tag")
* @JoinTable(name="blogs_tags",
* joinColumns={@JoinColumn(name="blog_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")}
* )
*/
protected $tags;
This is the code as in the official documentation of Doctrine as you can see in http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html