【问题标题】:ManyToOne ORM association in Doctrine with custom JoinColumn name具有自定义 JoinColumn 名称的 Doctrine 中的 ManyToOne ORM 关联
【发布时间】:2016-08-19 18:38:41
【问题描述】:

更新 01.09.2016 我更新了我的项目:我摆脱了我的实体中的自定义 JoinColumn(现在它生成了一个,并且我的项目工作没有错误)。我决定缩短问题

在与 Symfony 2.7、Doctrine 2.7 的 ManyToOne 单向关联中使用自定义命名的 JoinColumn 是个好主意吗?

我的情况是:


许多 ipGroup 实体(拥有许多IP 地址)应该与一个 ipGroupPrameters 实体 相关联。 (见下面的代码)


我正在尝试以 3 个变体运行代码(请参阅“工作流程”),并得到不同的错误:

1)。没有错误,但只是填充了 ipGroupParams 数据库表,并且没有在 ipGroup 表中插入任何行

2)。 “注意:未定义索引:ipGroupId”在 vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 第 685 行

$value = $newValId[$targetClass->fieldNames[$targetColumn]];

这真的很奇怪 - $targetClass 在 fieldNames 中有 ipGroupId(如调试器所示);

3)。 (在此变体中,我似乎更改了关系的 拥有 侧)DBAL 异常:执行“INSERT INTO ipGroup (ipGroupId, ip) VALUES (?, ?)”时发生异常params [null, "134.17.26.129"] ipGroupId 不能为空。 这很神奇,因为我在 $gObj 上应用了 setIpGroupId 方法,并且它在 RAM 中更新(我在调试器中看到它)。

工作流程是:

public function createIpGroup()
{
    $this->createParamsObj();
    $gObj = new \Bmw\StatBundle\Entity\ipGroup();
    $gObj->setIpGroupId($this->groupId);
    $gObj->setIp($ip);
    ### 1 variant
    $gObj->setIpGroupParams($this->paramsObj);
    ### 2 variant
    $gObj->setIpGroupParams($this->paramsObj);
    $this->em->persist($gObj); // if not rely on cascade persist
    ### 3 variant
    $this->paramsObj->addIpGroup($gObj);
    $this->em->persist($this->paramsObj);
    ###
    $this->em->flush();
}
public function createParamsObj()
{
    $pObj = new \Bmw\StatBundle\Entity\ipGroupParams();
    $this->groupId = $this->container->get('my.id.generator')->generate();
    $pObj->setIpGroupId($this->groupId);
    $this->em->persist($pObj);
    $this->em->flush();
    $this->paramsObj = $pObj;
}

这是初始 entities 代码,然后我运行 doc:gen:entities 来自动创建 getter 和 setter:

/**
 * ipGroup
 *
 * @ORM\Table(
 * uniqueConstraints={
 * @UniqueConstraint(name="ip", columns={"ip"}),
 * @UniqueConstraint(name="userId", columns={"userId"}),
 * },
 * indexes={@Index(name="ipGroupId", columns={"ipGroupId"})},
 * )
 * @ORM\Entity
 */
class ipGroup
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="ipGroupId", type="integer")
     */
    private $ipGroupId;

    /**
     * @ORM\ManyToOne(targetEntity="ipGroupParams", inversedBy="ipGroup")
     * @ORM\JoinColumn(name="ipGroupId", referencedColumnName="ipGroupId")
     */
    private $ipGroupParams;

    /**
     * @var string
     *
     * @ORM\Column(name="ip", type="string", length=45, nullable=true)
     */
    private $ip;
}

/**
 * ipGroupParams
 *
 * @ORM\Table(
 * uniqueConstraints={
 * @UniqueConstraint(name="ipGroupId", columns={"ipGroupId"}),
 * }
 * )
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class ipGroupParams
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @ORM\OneToMany(targetEntity="ipGroup", mappedBy="ipGroupParams", cascade={"persist", "remove"}, orphanRemoval=TRUE)
     */
    private $ipGroup;

    /**
     * @var integer
     *
     * @ORM\Column(name="ipGroupId", type="integer")
     */
    private $ipGroupId;
}

【问题讨论】:

  • 愚蠢的问题:你是否生成了实体的setter和getter?
  • 是的,@GeraldChablowski,getters/setters 生成的代码参见link

标签: php symfony orm doctrine model-associations


【解决方案1】:

Doctrine 不会自动链接这两个对象,但由于 persist 而将它们同时保存。这应该工作:

public function createIpGroup()
{
    $this->createParamsObj();
    $gObj = new \Bmw\StatBundle\Entity\ipGroup();
    $gObj->setIpGroupId($this->groupId);
    $gObj->setIp($ip);
    $gObj->setIpGroupParams($this->paramsObj);

    $this->paramsObj->addIpGroup($gObj);

    $this->em->persist($gObj);
    $this->em->flush();
}

【讨论】:

  • 谢谢,但它不起作用 - 就像 variant2。我还想,由于 ipGroupParams 实体中的 @OneToMany(..., cascade={"persist", "remove"}, ...) 符号,持久化会自动发生...
  • 我已对其进行了编辑并添加了 $gObj->setIpGroupId($this->groupId);。现在应该可以工作了。
  • 抱歉,@GeraldChablowski,但您没有添加任何行,您只是结合了 2 和 3'rd 变体
  • 我很抱歉没有帮助你。因为正常情况下,它应该可以工作。
猜你喜欢
  • 2011-09-07
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2023-03-31
相关资源
最近更新 更多