次のように、エンティティの1つにManyToOne関係があります。
class License {
// ...
/**
* Customer who owns the license
*
* @var \ISE\LicenseManagerBundle\Entity\Customer
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
// ...
}
class Customer {
// ...
/**
* Licenses that were at one point generated for the customer
*
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="License", mappedBy="customer")
*/
private $licenses;
// ...
}
これにより、ライセンステーブルの「customer_id」フィールドをnullにできるデータベーススキーマが生成されます。これは、まさに私が望まないことです。
これは、レコードを作成して、参照フィールドにnull値が実際に許可されていることを証明するコードです。
$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31"));
$license->setCreatedAt(new \DateTime());
// Persist the object
$em->persist($license);
$em->flush();
基本的に、顧客を割り当てずにライセンスを保持したくありません。設定する必要のある注釈はありますか、それとも、Customerオブジェクトをライセンスのコンストラクターに渡す必要がありますか?
私が使用しているデータベースエンジンはMySQLv5.1であり、Symfony2アプリケーションでDoctrine2を使用しています。