Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine2, pass Id or Object?
    primarykey
    data
    text
    <p>I do not understad why with some Entity objects I can set the Id and for others objects I get an error and says me that the Id can't be null and I have to pass an object instead.</p> <p>e.g.:</p> <pre><code>$log = new Log(); $log-&gt;setTypeId(1); $log-&gt;setUserId(1); $entityManager-&gt;persist($log); $entityManager-&gt;flush(); </code></pre> <p>If I try the code above I get error that says: <strong>Integrity constraint violation: 1048 Column 'user_id' cannot be null</strong>. And I have to first create the Type Object and de User object and the pass them:</p> <pre><code>$log-&gt;setType($TypeObject) $log-&gt;setUser($UserObject) </code></pre> <p>But for other entity objects I have no problem assigning the value directly, why is that?</p> <p>This is my Entity Log:</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="integer") */ protected $user_id; /** * * @var type * @Column(type="integer") */ protected $type_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 getUserId() { return $this-&gt;user_id; } public function getTypeId() { return $this-&gt;type_id; } public function getCreated() { return $this-&gt;created; } public function setUserId($userId) { $this-&gt;user_id = $userId; } public function setTypeId($typeId) { $this-&gt;type_id = $typeId; } 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()); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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