title: doctrine 2 persist entity and exclude null fields from insert query
body: <p>I am trying to add to all my doctrine generated entities two fields to handle dates (date_created and date_modified).</p>
  
  <p>Please see the current yml for reference</p>
  
  <pre><code>`Project\PasswordRecovery:
  type: entity
  table: PasswordRecovery
  lifecycleCallbacks:
    prePersist: [ prePersist ]
  indexes:
      fk_PasswordRecovery_User_idx:
          columns:
              - user_id
  id:
      id:
          type: integer
          generator:
              strategy: AUTO
  fields:
      date_created:
          type: datetime
          nullable: false
          columnDefinition: 'TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP'
          options:
              default: CURRENT_TIMESTAMP
      date_modified:
          type: datetime
          nullable: false
          columnDefinition: 'TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
          options:
              default: CURRENT_TIMESTAMP
      token:
          type: text
          nullable: true
      user_id:
          type: integer
          nullable: true
  manyToOne:
      users:
          targetEntity: User
          inversedBy: passwordRecoveries
          joinColumn:
              name: user_id
              referencedColumnName: id
  </code></pre>
  
  <p><strong>My problem:</strong></p>
  
  <p>As you can see in the sql table CREATE info below MYSQL can properly take care of handling current timestamp when inserting or updating queries.</p>
  
  <pre><code>CREATE TABLE `PasswordRecovery` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) DEFAULT NULL,
    `date_created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    `date_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `token` longtext COLLATE utf8_unicode_ci,
    PRIMARY KEY (`id`),
    KEY `fk_PasswordRecovery_User_idx` (`user_id`),
    CONSTRAINT `FK_41CD3A90A76ED395` FOREIGN KEY (`user_id`) REFERENCES `User` (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  CREATE TABLE `PasswordRecovery` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) DEFAULT NULL,
    `date_created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    `date_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `token` longtext COLLATE utf8_unicode_ci,
    PRIMARY KEY (`id`),
    KEY `fk_PasswordRecovery_User_idx` (`user_id`),
    CONSTRAINT `FK_41CD3A90A76ED395` FOREIGN KEY (`user_id`) REFERENCES `User` (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  </code></pre>
  
  <p>However when persisting the entity above even if I forcibly set to null $date_created and $date_modified when persisting, the generated query will in any case contain these two fields.
  This results in overriding the sql DEFAULT and ON UPDATE values.</p>
  
  <p>Firstly I forced these values to NULL to override CURRENT_TIMESTAMP default value which is imposed by datetime type in doctrine as default.</p>
  
  <pre><code>$entity-&gt;setDateCreated(null);
  $entity-&gt;setDateModified(null);
  </code></pre>
  
  <p>Then after setting the remaining values I persist:</p>
  
  <pre><code>$em-&gt;persist($entity);
  $em-&gt;flush();
  </code></pre>
  
  <p>By logging the query with doctrine's DebugStack I obtain the following:</p>
  
  <pre><code>Array
  (
      [1] =&gt; Array
          (
              [sql] =&gt; "START TRANSACTION"
              [params] =&gt; 
              [types] =&gt; 
              [executionMS] =&gt; 0.000178098678589
          )
  
      [2] =&gt; Array
          (
              [sql] =&gt; INSERT INTO PasswordRecovery (date_created, date_modified, token, user_id) VALUES (?, ?, ?, ?)
              [params] =&gt; Array
                  (
                      [1] =&gt; 
                      [2] =&gt; 
                      [3] =&gt; stackoverflow test insert
                      [4] =&gt; 14
                  )
  
              [types] =&gt; Array
                  (
                      [1] =&gt; datetime
                      [2] =&gt; datetime
                      [3] =&gt; text
                      [4] =&gt; integer
                  )
  
              [executionMS] =&gt; 0.00274181365967
          )
  
      [3] =&gt; Array
          (
              [sql] =&gt; "COMMIT"
              [params] =&gt; 
              [types] =&gt; 
              [executionMS] =&gt; 0.000401973724365
          )
  
  )
  </code></pre>
  
  <p><em>The sql result <strong>as INSERT statement</strong> from the db:</em></p>
  
  <pre><code>INSERT INTO `PasswordRecovery` (`id`, `user_id`, `date_created`, `date_modified`, `token`)
  VALUES
      (21, 14, NULL, NULL, 'stackoverflow test insert');
  </code></pre>
  
  <p><em>The expected result:</em></p>
  
  <pre><code>INSERT INTO `PasswordRecovery` (`id`, `user_id`, `date_created`, `date_modified`, `token`)
  VALUES
      (23, 14, '2017-02-23 08:28:23', '2017-02-23 08:28:23', 'stackoverflow test insert');
  </code></pre>
  
  <p><strong>So far I haven't found a way to skip inserting a field</strong> based on some condition or the data. Setting 'nullabe' to false or true didn't seem to make any difference.</p>
  
  <p>Using query builder is not an option as the EntityManager persitence scheme is deeply rooted in the system I'm working on.</p>
  
  <p>Also using datetime in mysql is not an option as well for this specific project.</p>
  
  <p>I have seen similar threads regarding dates management but in this case <strong>the problem regards only the fact that I'm unable to completely let MYSQL take care about generating and updating these two date fields as doctrine persist/flush will always overwrite these with the generated INSERT statement.</strong></p>
  
  <p>I am looking into lifecycle callbacks to see if these can help in not inserting some specific field based on a number of parameters.</p>
  
  <p>The next step will be fiddling with the orm source.</p>
  
  <p>I hope someone can help with the above !</p>