无法获取整个实体,无法直接访问关联实体的保存ID [英] Cannot access the saved ID of the associated entity directly without getting entire entity

查看:136
本文介绍了无法获取整个实体,无法直接访问关联实体的保存ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解为什么我不能访问我的实体的id字段,当它是相关联的ID的引用。我已经看到这是这种情况,但是我想知道为什么给出一个实际的吸气剂的存在,应该拉数据库中的数据。



特别是这是一个问题,因为我需要根据这个id查询我的实体,例如

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

我有一个文章到具有单向OneToOne关联的 ViewVersion 实体(因为viewVersion可以指向很多不同的内容类型,我不能硬编码为双向...我不知道这是我有另一个问题提出)。这被保留到数据库,并且在数据库中设置了一个 viewVersionId (我在那个字段中看到它)。当我从数据库中获取实体时,我可以转储实体并查看所有字段,但是 viewVersionId 报告为NULL,即使它的值为29数据库!

 类文章{

/ **
* @ 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;
}
}

如果我提取实体并尝试打印出viewVersionId直接,它为null:

  //对于id为14的硬编码提取,因为$ articleRepo-> findOneByViewVersionId(29)doesn' t work 
$ content = $ articleRepo-> find(14);
print('< h1>查看版本号:'$ content-> getViewVersionId()。'(= NULL)< / h1>');
print('< h1>查看版本号:'$ content-> getViewVersion() - > getId()。'(= 29)< / h1>');

\Doctrine\Common\Util\Debug :: dump($ content,1);
//打印所有字段,但是viewVersionId为NULL
object(stdClass)#5095(14){
[__CLASS __] => string(38)Gutensite\ArticleBundle\Entity\Article
[content] => string(10)第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
}

所以为什么我不能直接访问viewVersionId值,如果数据是坚持到我的数据库?我认为这是因为OneToOne协会,但在这种情况下,数据是在我的实体,我有一个getter,所以为什么它不工作?

ORC(解决方案)

$ viewVersionId 您需要添加正确的注释,以便在Doctrine从数据库中获取Entity时填充。



尝试:



pre $ / **
* @ ORM\Column(type =integer)
* /
pirvate $ viewVersionId;


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?

解决方案

$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;

这篇关于无法获取整个实体,无法直接访问关联实体的保存ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆