gpt4 book ai didi

php - 学说 2 : Insert with to-many relation: referenced row is not inserted

转载 作者:行者123 更新时间:2023-11-30 22:30:00 25 4
gpt4 key购买 nike

我有两个实体 A 和 B,A 有很多 B(从 A 到 B 的单向多对多)。创建新 A 并为其分配 B 时,连接表上的约束失败,因为学说试图为新 A 插入 ID 0 的关联。这让我相信 INSERT那时还没有执行新的A。

/**
* @Entity
* @Table(name="a")
*/
class A {
/**
* @var int
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @var B[]
* @ManyToMany(targetEntity="B", fetch="LAZY")
* @JoinTable(name="jointable",
* joinColumns={@JoinColumn(name="a_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="b_id", referencedColumnName="id")}
* )
*/
protected $bs;

public function getBs() { return $this->bs; }
}

// I omit B here, because it is not important

// controller code:
$a = new A();
$a->getBs()->add($em->find(B::class, 8));
$em->persist($a);
$em->flush();

这是我得到的错误:

An exception occurred while executing 'INSERT INTO jointable (a_id, b_id) VALUES (?, ?)' with params [0, 8]:

SQLSTATE[23000]: Integrity constraint violation...

查看数据库(和查询日志),从未创建 A。因此,与 B #8 的关联失败。

我怎样才能使教义正确无误?

最佳答案

事实证明,学说做的一切都是正确的。问题是我报告的这个 PHP 错误:https://bugs.php.net/bug.php?id=71059

要使用 Doctrine 来补偿它,您有两个选择:

覆盖 bool 类型

如果您的数据库中的所有 bool 字段都是 TINYINT(对我来说就是这样),您可以简单地覆盖 bool 类型:

Type::overrideType(Type::BOOLEAN,    \Doctrine\DBAL\Types\TinyintBooleanType::class);

为 TINYINT bool 值添加自定义类型

如果您的数据库中并非所有 bool 字段都是 TININT,您必须向 doctrine 添加新类型:

Type::addType(
\Doctrine\DBAL\Types\TinyintBooleanType::NAME,
\Doctrine\DBAL\Types\TinyintBooleanType::class
);

$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping(
Type::SMALLINT, \Doctrine\DBAL\Types\TinyintBooleanType::NAME
);

然后您可以像这样在实体中使用此类型:

@Column(type="tinyintasbool")

TinyintBooleanType 来源

<?php
namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* This type is appropriate when storing boolean values in TINYINT columns.
* @author Tobias Marstaller
*/
class TinyintBooleanType extends Type
{
const NAME = "tinyintasbool";

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null)
{
return null;
}
else return $value? 1 : 0;
}

public function convertToPHPValue($value, AbstractPlatform $patform)
{
if ($value === null)
{
return null;
}
else return ((int) $value) === 0? false : true;
}

/**
* Gets the SQL declaration snippet for a field of this type.
*
* @param array $fieldDeclaration The field declaration.
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform.
*
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getSmallIntTypeDeclarationSQL($fieldDeclaration);
}

/**
* Gets the name of this type.
*
* @return string
*
* @todo Needed?
*/
public function getName()
{
return static::NAME;
}

public function getBindingType()
{
return \PDO::PARAM_INT;
}
}

关于php - 学说 2 : Insert with to-many relation: referenced row is not inserted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com