1

I recently started using Symfony2-Doctrine2. I'm not getting how to save data in inheritance mapping.

My requirements:

For learning exercise:

  • I'm making a library application for testing (Requirements might not be practical).
  • At high level, library may contain many different type of items like books, articles, manuals as example for now.
  • They have some common fields like name, publish year etc and some item specific details like book has IDBN, publisher; Manual have company, product.
  • Again to make problem little more complex, there is another 'item_content' table to have some description in different language.

To quickly visualize, I've following structure:

enter image description here

I achieved above structure as per doctrine docs for inheritance mapping & Bidirectional one to many relation

My Question: How to save data using Symfony2 (I've proper routing/actions running, just need code to write in controller or better in repository). While saving data (say for manual) I want to save data in Item, Manual and ItemContect table but getting confused due to discr field in database. I didn't find code for saving data in above structure. I don't need full code, just few hints will be sufficient. My Item class is as follow (Other classes have proper inverse as mentioned in doctrine docs):

/**
 * Article
 *
 * @ORM\Table(name="item")
 * @ORM\Entity(repositoryClass="Test\LibraryBundle\Entity\ItemRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"book" = "Book", "manual" = "Manual", "article" =  "Article"})
 */
class Item
{
    //...

    /**
     *  For joining with ItemContent
     * 
     * @ORM\OneToMany(targetEntity="ItemContent", mappedBy="item")
     **/
    private $itemContents; 

    public function __construct()
    {
        $this->itemContents = new ArrayCollection();
    }

    //...
}

1 Answer 1

2

The discriminator field will be automatically filled by Doctrine

$em = $this->getDoctrine()->getManager();
$item = new Manual(); // discr field = "manual"
$itemContent = new ItemContent();

$item->addItemContent($itemContent);
$itemContent->setItem($item);

$em->persist($item);
$em->persist($itemContent);
$em->flush();

Is that the answer you're waiting ?

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

3 Comments

Thanks @VBee Ahh yes, my bad. Didn't consider $item = new Manual(); I was creating separate objects for Item and Manual. But this raised another doubt, I don't want to write that code in controller but repository. So where $item = new Manual(); belongs? ItemRepository or ManualRepository.
ManualRepository doesn't exist, and the best practice is to create a custom entity manager (like github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Model/…). I advise you to put this logic in your controller in a first time.
sorry, you can create ManualRepository, but my advise is the same, use repositories for fetching data only.

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.