Renato Dinhani
Renato Dinhani

Reputation: 36746

How NOT persist object inside entity with Doctrine?

I have the following class that is persisted using Doctrine, but I don't want that the knowledgeArea and knowledgeLevel attributes be inserted because they are objects, not attributes.

What I need to do to not insert these objects, but only the attributes with the reference to they?

NOTE: I'm learnig Doctrine, I think that I'm doing something wrong, but I'm not sure. And I'm using MySQL.

class Knowledge {

    /** @Id @Column(name="id", type="bigint") @generatedValue(strategy="AUTO") **/    
    private $id;

    /** @Column(name="name", type="string") **/
    private $name;

    /** @Column(name="description", type="text") **/
    private $description;

    /** @Column(name="exibition_order", type="integer") **/
    private $exibitionOrder;

    /**         
    * @ManyToOne (targetEntity="KnowledgeArea", inversedBy="knowledges")
    * @JoinColumn(name="knowledge_area_id", referencedColumnName="id")
    **/
    private $knowledgeArea; #this is an object

    /** 
    * @ManyToOne (targetEntity="KnowledgeLevel", inversedBy="knowledges")
    * @JoinColumn(name="knowledge_level_id", referencedColumnName="id")    
    **/
    private $knowledgeLevel; #this is an object

Upvotes: 3

Views: 1676

Answers (1)

gview
gview

Reputation: 15411

So it seems that the question you have is how to set the foreign key relationships? The typical way is to use references. Inside a Symfony2 controller as an example you would have:

$em = $this->getDoctrine()->getEntityManager();
$knowledge = new Knowledge();
$knowledge->setKnowledgearea($em->getReference('AcmeBundle:Knowledges', 3));

If you're using some other framework, you need the doctrine entitymanager object, but the idea is the same.

This is documented here.

Upvotes: 2

Related Questions