4

I don't know if this is a bug, but I am using Doctrine 2.3.0 and I found the persist/flush behaviour quite strange. I have a basic table:

------------------
|   test         |
------------------
| id   INT | AI  |
| field1 VARCHAR |
| field2 VARCHAR |
-----------------

When I create an entry by setting only field1:

$test = new Entities\test();
$test->setField1('foo');
$em->persist($test);
$em->flush();

Doctrine\DBAL\Logging\EchoSQLLogger tells me (which is confirmed by looking at the DB) that Doctrine performs the following query:

INSERT INTO test (field1, field2) VALUES (?, ?)
array(2) {
  [1]=>
  string(3) "foo"
  [2]=>
  NULL
}

As you can see, although I haven't set field2, Doctrine does put it in the insert statement with a NULL value.

I have that behaviour for all my entities, which is problematic because when doing inserts, my default DB values for fields I don't set are overwritten by NULL.

Is this the expected default behaviour of Doctrine, is there a way to turn that off (i.e. exclude fields I don't set from the INSERT statements)?

I should probably add that my entities were generated automatically with the reverse engineering, and that the declaration for one of the fields looks like this

/**
 * @var string $field2
 *                                       // removing nullalble makes no diff.
 * @ORM\Column(name="field2", type="string", length=45, nullable=true)
 */
private $field2;

/**
 * Set field2
 *
 * @param string $field2
 * @return Test
 */
public function setField2($field2)
{
    $this->field2 = $field2;
    return $this;
}
4

1 に答える 1

1

1 つの INSERT ステートメントで完全なレコードを作成することは論理的に扱われる可能性があるため、これがデフォルトの動作であると思います。しかし、私は間違っている可能性があります。ある種の@ORM\Column(type="boolean", columnDefinition="TINYINT(1) NULL DEFAULT 0")ハックとして扱うことができますが、解決策です。データベース レコードの挿入ロジックを確認することをお勧めします。

于 2012-10-24T08:28:05.257 に答える