Note that there are some explanatory texts on larger screens.

plurals
  1. POInterchange a raw value in Doctrine 2 result
    text
    copied!<h3>The Problem:</h3> <p>I am working on a WordPress Symfony bundle and creating entities for it. </p> <p>I have a <code>Comment</code> entity, and <code>$comment-&gt;user</code> is mapped to an <code>User</code> entity.</p> <p>However <strong>WordPress use <code>0</code> to represent a guest user</strong>. It cause a lots of problems in Doctrine because the user with id zero never exist. It cause the following issues:</p> <ul> <li><code>$comment-&gt;getUser()</code> might throw an entity not found exception when the row's <code>user_id</code> is <code>0</code>.</li> <li><code>$comment-&gt;setUser()</code> doesn't work because you can not use <code>null</code> to repensent guest (should use <code>0</code>), and you cannot use <code>new User(0)</code> neither.</li> </ul> <h3>The Question:</h3> <p>By default, the following code would save <code>null</code> to <code>user_id</code> column in database:</p> <pre><code>$comment-&gt;setUser(null); </code></pre> <p>Is it possible to make Doctrine save <code>0</code> (instead of <code>null</code>) to the <code>user_id</code> column?</p> <p>Or even better, can I interchange <code>0</code> and <code>null</code> when dealing with the <code>user_id</code> column?</p> <p>Thank you for your time.</p> <h3>Test Cases:</h3> <pre><code>// if a guest posted a comment, pass null to setUser() // although the actual value will be saved to user_id column is 0 $guestComment-&gt;setUser(null); // if a comment was posted by a guest, getUser() should return null // although the actual value returned by user_id column is 0 $guestComment-&gt;getUser(); // return null // if a member posted a comment, pass a User entity to setUser() $memberComment-&gt;setUser(new User()); // if a comment was posted by a member, getUser() should return the User entity $guestComment-&gt;getUser(); // return User entity. </code></pre> <h3>Direction:</h3> <p>I am looking at creating a custom mapping types <a href="http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html" rel="nofollow">http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html</a></p>
 

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