Gui*_*ois 6 mysql doctrine doctrine-orm zend-framework2
对于与Doctrine 2的关系(多对一,一对一),是否可以使用值0而不是null?
现在我有很多NOT NULL列,我可能不会更改为空值.将MySQL中的默认值更改为0自身不是解决方案becease doctrine始终设置用于插入/更新行的列.
Ocr*_*ius 10
不,这是不可能的.
NULL在SQL中有一个非常具体的含义.它代表"没有价值",你可以验证你的逻辑即使在SQL级别也不会工作:
CREATE TABLE foo (
`id` INT(11) PRIMARY KEY AUTO_INCREMENT,
`bar_id` INT(11)
);
CREATE TABLE `bar` (`id` INT(11) PRIMARY KEY AUTO_INCREMENT);
ALTER TABLE foo ADD FOREIGN KEY `bar_id_fk` (`bar_id`) REFERENCES `bar` (`id`);
INSERT INTO `bar` VALUES (NULL);
INSERT INTO `bar` VALUES (NULL);
INSERT INTO `bar` VALUES (NULL);
INSERT INTO `foo` VALUES (NULL, 1);
INSERT INTO `foo` VALUES (NULL, 2);
INSERT INTO `foo` VALUES (NULL, 3);
INSERT INTO `foo` VALUES (NULL, 0);
/*
ERROR 1452 (23000):
Cannot add or update a child row: a foreign key constraint fails
(`t2`.`foo`, CONSTRAINT `foo_ibfk_1` FOREIGN KEY (`bar_id`)
REFERENCES `bar` (`id`))
*/
INSERT INTO `foo` VALUES (NULL, 4);
/*
ERROR 1452 (23000):
Cannot add or update a child row: a foreign key constraint fails
(`t2`.`foo`, CONSTRAINT `foo_ibfk_1` FOREIGN KEY (`bar_id`)
REFERENCES `bar` (`id`))
*/
INSERT INTO `foo` VALUES (NULL, NULL); /* VALID! */
Run Code Online (Sandbox Code Playgroud)
所以不,你不能让Doctrine ORM表现得那么0被解释为NULL,因为RDBMS本身不允许这样做.
你可以做的是在你的数据库中插入"假的"引用条目,当水合作为实体时,它将作为空对象:
INSERT INTO `bar` VALUES (NULL);
UPDATE `bar` SET `id` = 0 WHERE `id` = 4;
INSERT INTO `foo` VALUES (NULL, 0); /* now works! */
Run Code Online (Sandbox Code Playgroud)
在实体方面,它看起来非常相似.
(请注意,publicDoctrine ORM 2.4仅支持属性,但尚未发布.但它们使这里的内容更容易阅读)
Foo.php:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table(name="foo")
*/
class Foo
{
/**
* @ORM\Column(type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="Bar")
* @ORM\JoinColumn(name="bar_id", referencedColumnName="id", nullable=false)
*/
public $bar;
}
Run Code Online (Sandbox Code Playgroud)
Bar.php:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table(name="bar")
*/
class Bar
{
/**
* @ORM\Column(type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
}
Run Code Online (Sandbox Code Playgroud)
然后是生成新Foo实例的代码:
$nullBar = $entityManager->find('Bar', 0);
$foo = new Foo();
$foo->bar = $nullBar;
$em->persist($foo);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3618 次 |
| 最近记录: |