Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong></p> <p>I found this statement on the website of Doctrine2. It's a best practice that you might want to follow when coding your models.</p> <p><a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/best-practices.html" rel="nofollow">Doctrine2 Best Practices</a></p> <p>25.9. Don’t map foreign keys to fields in an entity</p> <p>Foreign keys have no meaning whatsoever in an object model. Foreign keys are how a relational database establishes relationships. Your object model establishes relationships through object references. Thus mapping foreign keys to object fields heavily leaks details of the relational model into the object model, something you really should not do</p> <p><strong>EDIT</strong></p> <p>Doctrine does the mapping from your objects to their respective Ids. </p> <p>What you've done here is a bit redundant.</p> <p>You've essentially told doctrine the same thing twice.</p> <p>You've told it that it has a 'user_id' column AND that it also has a User object, which are the same thing. But doctrine can already guess that this relationship will have a user_id column based on the fact that the log class has a user object inside.</p> <p>You should simply do the following instead</p> <pre><code>&lt;?php /** * @Entity * @Table(name="log") * @HasLifecycleCallbacks */ class Log { /** * @var type * @Id * @Column(type="integer") * @GeneratedValue */ protected $id; /** * * @var type * @Column(type="datetime") */ protected $created; /** * * @var type * @ManyToOne(targetEntity="User", inversedBy="logs") */ protected $user; /** * * @ManyToOne(targetEntity="Type", inversedBy="logs") */ protected $type; public function getId() { return $this-&gt;id; } public function getCreated() { return $this-&gt;created; } public function setCreated($created) { $this-&gt;created = $created; } public function setUser($user) { $this-&gt;user = $user; } public function setType($type) { $this-&gt;type = $type; } /** * @PrePersist */ public function prePersist() { $this-&gt;setCreated(new DateTime()); } } </code></pre> <p>Doctrine will worry about the user_id and type_id on it's own. You don't have to worry about it. This way you get to work with full fledged objects, making it easier to program, instead of having to worry about id's. Doctrine will handle that.</p> <p>If ALL you have is an id, because that's what you're using on the front end, then just fetch the object associated with that id using the Entitymanager.</p> <pre><code>$user = $em-&gt;getEntity( 'User', $idFromWeb ); $log = new Log(); $log-&gt;setUser( $user ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload