0

I am trying to find all results in the Monthly table that have an association with a Category. I did my research and the following code should theoretically speaking work but somehow is throwing the following Error Notice: Undefined index: joinColumns in...

The tables are joined by a join table and the keys used is category_id and monthly_id tried to run the query using category_id but still doesnt work and throws another error.

When I run orm:validate-schema it seems all to be working fine. What am I missing?

From the Monthly Entity

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Bnk\Entity\Category", inversedBy="monthly")
     * @ORM\JoinTable(name="months_categories",
     *   joinColumns={
     *     @ORM\JoinColumn(name="monthly_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *   }
     * )
     */
    private $category;

What am i missing so that I can find all Monthly that are associated with a given Category?

$months = $this->getEntityManager()
    ->getRepository('Bnk\Entity\Monthly')
    ->findBy(
        array(
            "category"=>$category->getId()
        )
);
Wilt
  • 41,477
  • 12
  • 152
  • 203
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • are you sure it fails on this specific relation? try and removing this relation config and see if the error disappears. – NDM Aug 29 '14 at 10:38
  • @NDM if i do findAll it works fine – Jonathan Thurft Aug 29 '14 at 11:02
  • findBy cannot handle a many-to-many relation. http://stackoverflow.com/questions/13343533/using-entityrepositoryfindby-with-many-to-many-relations-will-lead-to-a-e-no – Cerad Aug 29 '14 at 13:27
  • And consider renaming Monthly#category to categories. Makes it clear that you dealing with an array of categories. – Cerad Aug 29 '14 at 13:37

1 Answers1

0

Did you maybe forget to initialize your ArrayCollection? In your entity constructor function you always have to initialize them.

So at the top of your class:

use Doctrine\Common\Collections\ArrayCollection;

and your constructor function:

public function __construct()
{
    $this->category= new ArrayCollection();
}
Wilt
  • 41,477
  • 12
  • 152
  • 203