OneToOne 主义让我发疯

Doctrine OneToOne drives me crazy

我知道这是一个反复出现的问题,但在 2 个实体和 运行 'php app/console doctrine:schema:validate' 之间创建一对一关系后,我收到此错误:

'[Mapping]  FAIL - The entity-class 'HO\CisBundle\Entity\AffiliateSalesAccounts' mapping is invalid:
* The association HO\CisBundle\Entity\AffiliateSalesAccounts#affiliate refers to the inverse side field HO\HasoffersBundle\Entity\Affiliate#affiliateSalesAccounts which does not exist.'

这是包含两个实体的代码的一部分:

AffiliatesSalesAccount 实体

namespace HO\CisBundle\Entity;

use HO\HasoffersBundle\Entity\Affiliate;
use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Table(name="AffiliateSalesAccounts")
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks
 */
class AffiliateSalesAccounts {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    private $id;

    /**
     * @var \HO\HasoffersBundle\Entity\Affiliate
     *
     * @ORM\OneToOne(targetEntity="HO\HasoffersBundle\Entity\Affiliate", inversedBy="affiliateSalesAccounts")
     * @ORM\JoinColumn(name="affiliate_id", referencedColumnName="id")
     *
     */
    private $affiliate;

    ...

    /**
     * @param Affiliate $affiliate
     */
    public function setAffiliate(Affiliate $affiliate)
    {
        $this->affiliate = $affiliate;
    }

    /**
     * @return Affiliate
     */
    public function getAffiliate()
    {
        return $this->affiliate;
    }
}

关联实体

namespace HO\HasoffersBundle\Entity;

use HO\CisBundle\Entity\AffiliateSalesAccounts;
use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Table(name="ho_Affiliate")
 */
class Affiliate
{


    /**
     * @var AffiliateSalesAccounts
     *
     * @ORM\OneToOne(targetEntity="HO\CisBundle\Entity\AffiliateSalesAccounts", mappedBy="affiliate")
     */
    private $affiliateSalesAccounts;


    ....

    /**
     * @param \HO\CisBundle\Entity\AffiliateSalesAccounts $affiliateSalesAccounts
     */
    public function setAffiliateSalesAccounts($affiliateSalesAccounts)
    {
        $this->affiliateSalesAccounts = $affiliateSalesAccounts;
    }

    /**
     * @return \HO\CisBundle\Entity\AffiliateSalesAccounts
     */
    public function getAffiliateSalesAccounts()
    {
        return $this->affiliateSalesAccounts;
    }

}

我在 2 个实体之间有其他类似的 OneToOne 关系,并且效果很好。

有人可以帮我吗?

非常感谢..

我不确定你的问题,但我注意到我用不同的方式来解决我只是把代码放在这里,这样你就可以检查它

/**
 * @ORM\OneToOne(targetEntity="Profile", mappedBy="user", cascade={"remove"})
 **/
private $profile;

另一个class:

/**
 * @var \Lifecrops\LCUserBundle\Entity\User
 *
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\OneToOne(targetEntity="Lifecrops\LCUserBundle\Entity\User", inversedBy="profile")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="User_id", referencedColumnName="id")
 * })
 */
private $user;

通常我会这样做:

namespace HO\CisBundle\Entity;

use HO\HasoffersBundle\Entity\Affiliate;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="AffiliateSalesAccounts")
 * @ORM\Entity()
 */
class AffiliateSalesAccounts {

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Affiliate", inversedBy="affiliateSalesAccounts")
     * @ORM\JoinColumn(name="affiliate_id", referencedColumnName="id")
     *
     */
    private $affiliate;

    /**
     * @param Affiliate $affiliate
     */
    public function setAffiliate(Affiliate $affiliate)
    {
        $this->affiliate = $affiliate;
    }

    /**
     * @return Affiliate
     */
    public function getAffiliate()
    {
        return $this->affiliate;
    }
}

第二个实体:

namespace HO\HasoffersBundle\Entity;

use HO\CisBundle\Entity\AffiliateSalesAccounts;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="ho_Affiliate")
 * @ORM\Entity()
 */
class Affiliate
{


    /**
     * @ORM\OneToOne(targetEntity="AffiliateSalesAccounts", mappedBy="affiliate")
     */
    private $affiliateSalesAccounts;

    /**
     * @param AffiliateSalesAccounts $affiliateSalesAccounts
     */
    public function setAffiliateSalesAccounts($affiliateSalesAccounts)
    {
        $this->affiliateSalesAccounts = $affiliateSalesAccounts;
    }

    /**
     * @return AffiliateSalesAccounts
     */
    public function getAffiliateSalesAccounts()
    {
        return $this->affiliateSalesAccounts;
    }

}

清除缓存是重要的部分,如果一切都不好,你可以看看:

doctrine:
  orm:
    auto_mapping: true

好的,我解决了问题...首先,很抱歉打扰你,因为这是一个愚蠢的愚蠢的心不在焉...因为我们使用 Redis 来缓存学说(使用 sncRedisBundle),特别是元数据和查询,Affiliate实体已缓存在 Redis 中,但没有新字段声明 "affiliateSalesAccounts"。删除存储关联实体的密钥后问题解决了。

非常感谢您的评论