【问题标题】:Doctrine 2, 1:M bidirectional persist doesn't work properlyDoctrine 2, 1:M 双向持久性无法正常工作
【发布时间】:2013-11-13 01:41:45
【问题描述】:

我正在尝试在 1:M 双向关联中使用原则 2 坚持到拥有方。由于某些奇怪的原因(至少对我来说很奇怪),我没有将 accountId 值放入插入语句中,因此它失败了。我不确定我在这里是否做错了什么。

谁能指出发生了什么。

** 使用 Zend Framework 2 + ubuntu + mysql。

表 tAccounts 键是 accountId,并且该表通过外键 accountId 连接到 tAccountPasswordReset

(拥有方)。

class tAccountPasswordReset {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $Id;
/** @ORM\Column(type="integer") */
protected $accountId;
.......
.......
/** 
* @ORM\ManyToOne(targetEntity="tAccounts", inversedBy="accountpasswordreset")
* @ORM\JoinColumn(name="accountId", referencedColumnName="accountId")
**/
private $accounts;

(反面)

class tAccounts {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $accountId;
/** @ORM\Column(type="string") */

.... ....

/** @ORM\OneToMany(targetEntity="tAccountPasswordReset", mappedBy="accounts") */
private $accountpasswordreset;

public function __construct() {
    /** Get the dependence rowset from the tAccountPasswordReset */
    $this->accountpasswordreset = new ArrayCollection();
}

在 tAccountPaswordReset 中插入一行 (我很肯定 accountId 不为空

private function setResetToken($accountid) {
    try {
        $token = uniqid().uniqid(); /** Generate a unique token */
        $newReset = new \Entity\Tables\tAccountPasswordReset();
        $newReset->accountId = $accountid;
        $newReset->resetToken = $token;
        $this->entityManager->persist($newReset);
        $this->entityManager->flush();
        return $token;
    } catch (Exception $e) {
        throw $e;
    }
}

(结果)

An exception occurred while executing '
INSERT INTO tAccountPasswordReset (accountId, resetToken, createTime, expTime) 
VALUES (?, ?, ?, ?)' with params [null, "5273f94bd75f15273f94bd7641", "2013-11-01 14:56:11", "2013-11-02 14:56:11"]: SQLSTATE[23000]: 
Integrity constraint violation: 1048 Column 'accountId' cannot be null

【问题讨论】:

    标签: php doctrine-orm zend-framework2 associations persist


    【解决方案1】:

    从这里看起来很简单,您没有将 AccountID 类型的 Object 设置为 tAccountPasswordReset

    表示您需要设置或显式地将 tAccounts 对象传递给 tAccountPasswordReset 的函数,该函数的作用类似于 setter/getter。

    我建议你在 tAccountPasswordReset

    中做这样的事情
    public function getTAccount(){
     return $this->AccountID;
     } 
    
     public function setTAccount((complete path to this class)/tAccounts $tAccounts){
     $this->AccountID = $tAccounts;
    }
    

    然后在您的 setResetToken 函数中,将 tAccounts 对象设置为像这样的 setter

      $newReset->setTAccount(pass tAccounts object here);// if you dont know how to set object here comment below
    

    其余的工作将由 Doctrine 处理。

    PS:目前在我看来它不是双向的,但如果是的话,我可能会理解它。

    【讨论】:

    • tAccountPasswordResettAccountscalling function。现在,当我尝试将记录添加到 tAccountPasswordReset 表时,它会尝试将记录添加到 tAccounts 表中。可能在这种情况下我不需要定义 1:M 双向关系。我可能只需要 1:M 单向。在那种情况下,我可能会误解 Doctrine 中单向/双向关联之间的区别。
    • 让我们清楚我在这里尝试做的事情。我只需要在 tAccountPasswordReset 表中添加一条记录,该表通过 accountId 与 tAccounts 表链接(accountId 是 tAccounts 表中的主键和 tAccountPasswordReset 表中的 FK)。
    • 我已经得到了你想要的,但是你需要明白这一点,如果你想做你在之前评论中所说的,那么你需要将 Taccount 对象传递给 tAccountPasswordReset
    猜你喜欢
    • 2016-07-16
    • 2013-11-05
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多