0

I nead help.

I have 3 entities. Book, Category And BookCategory - book can have multiple categories so i used another table.

I can easily acces Book and Category useing BookCategory table but i dont know how to do this by Book->BookCategory->Category.

    class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="text")
     */
    private $name;

     /**
      * @var Category
      * @ORM\ManyToOne(targetEntity="Category", inversedBy="Category")
      * @ORM\JoinColumn(name="parent", referencedColumnName="id")
      */
    private $parent;

class BookCategory
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

     /**
      * @var Book
      * @ORM\ManyToOne(targetEntity="Book", inversedBy="BookCategory")
      * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
      */
    private $bookId;

     /**
      * @var Category
      * @ORM\ManyToOne(targetEntity="Category", inversedBy="BookCategory")
      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
      */
    private $categoryId;

    /**
     * @var integer
     *
     * @ORM\Column(name="priority", type="integer")
     */
    private $priority;

class Book
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="text")
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="author", type="text")
     */
    private $author;

    /**
     * @var float
     *
     * @ORM\Column(name="price", type="float")
     */
    private $price;

How i need to config my entities or how to make my DQL to achive wanted results?

2
  • 1
    In Addition to what @TimoSta said: You need to set the inversedBy attributes of the annotations correctly. Currently you should be getting errors because they should be set to a property of the entity in targetEntity not an entity itself. Commented Jul 21, 2014 at 8:45
  • @RoToRa can you show me how to do this? Commented Jul 21, 2014 at 11:37

1 Answer 1

2

With your code, you only established the relationship from BookCategory to Book. As you said, that enables you to get the Book associated to one BookCategory. To go the other way and get all BookCategory that belong to one book, you also need to specify this relationship. What you want is a OneToMany relationship from Book to BookCategory.

<?php

//...
use Doctrine\ORM\Mapping\OneToMany;

class Book
{
    //...

    /**
    * @OneToMany(targetEntity="BookCategory", mappedBy="bookId")
    */
    private $bookCategories;

    //...
}

class BookCategory
{

     //...

     /**
      * @var Book
      * @ORM\ManyToOne(targetEntity="Book", inversedBy="bookCategories")
      * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
      */
    private $bookId;

     //...

}

After adding the necessary getters and setters, getBookCategories() will give you an Array with all BookCategory that belong to the Book.

For more details, have a look at the official Symfony2 documentation: http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata

Edit: Included use statement. Corrected inversedBy property for bookId.

Sign up to request clarification or add additional context in comments.

2 Comments

Now im getting Annotation @onetomany was never imported when i try to generate entities.
That's probably because you did not import the Annotation via use. Have a look at my edit. Also make sure you take into account what @RoToRa commented to your question. The inversedBy property of the ManyToOne relationship of the property bookId of your Book class has to be adapted. I also corrected it in my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.