Cannot access the saved ID of the associated entit

2019-08-05 03:26发布

I would like to understand why I cannot access the id field in my entity, when it is reference for an associated ID. I have read that this is the case, but I would like to know why given, the presence of an actual getter that should pull the data which IS in the database.

An in particular this is a problem, because I need to query my entity BASED on this id, e.g.

$article = $articleRepo->findOneByViewVersionId($view->getVersion()->getId());

I have an Article entity, that points to a ViewVersion entity with a uni-directional OneToOne association (because the viewVersion can point to a lot of different content types, I can't hard code this as bidirectional... I don't know how that's another question I've posed). This gets persisted to the database and a viewVersionId is set in the database (I see it there in that field). When I fetch the entity from the database, I can dump the entity and see ALL the fields, but the viewVersionId is reported as NULL, even though it has a value of 29 in the database!

class Article {

    /**
     * @ORM\OneToOne(targetEntity="\Gutensite\CmsBundle\Entity\View\ViewVersion")
     * @ORM\JoinColumn(name="viewVersionId", referencedColumnName="id")
     */
    protected $viewVersion;

    protected $viewVersionId;

    public function getViewVersion() {
        return $this->viewVersion;
    }

    public function getViewVersionId() {
        return $this->viewVersionId;
    }
}

If I fetch the entity and try to print out the viewVersionId directly, it's null:

// hard coded fetch for id 14 since $articleRepo->findOneByViewVersionId(29) doesn't work
$content = $articleRepo->find(14);
print('<h1>View version ID: '.$content->getViewVersionId().' (= NULL) </h1>');
print('<h1>View version ID: '.$content->getViewVersion()->getId().'(= 29) </h1>');

\Doctrine\Common\Util\Debug::dump($content, 1); 
// this prints out all the fields but the viewVersionId is NULL
object(stdClass)#5095 (14) {
    ["__CLASS__"]=> string(38) "Gutensite\ArticleBundle\Entity\Article"
    ["content"]=> string(10) "Article 14"
    ["profileId"]=> NULL
    ["id"]=> int(14)
    ["lockVersion"]=> int(1)
    ["siteId"]=> NULL
    ["time"]=> int(1399935757)
    ["timeMod"]=> NULL
    ["timeDelete"]=> NULL
    ["flagDelete"]=> bool(false)
    ["userId"]=> NULL
    ["userIdMod"]=> NULL
    ["viewVersion"]=> string(43) "Gutensite\CmsBundle\Entity\View\ViewVersion"
    ["viewVersionId"]=> NULL
}

So why can't I access the viewVersionId value directly if the data is persisted to my database? I assume it's because of the OneToOne association, but in this case, the data is in my entity, and I have a getter, so why doesn't it work?

1条回答
smile是对你的礼貌
2楼-- · 2019-08-05 03:52

$viewVersionId in your entity is not visible to ORM at all. You need to add the correct annotation for it to be populated when Doctrine fetches the Entity from the database.

Try:

/**
 * @ORM\Column(type="integer")
 */
pirvate $viewVersionId;
查看更多
登录 后发表回答