简体   繁体   English

访问Symfony2中的相关实体

[英]Accessing Related Entities in Symfony2

I have a View entity that references an associated entity called ViewVersion . 我有一个View实体,该实体引用了一个称为ViewVersion的关联实体。 But if I name the variable anything other than viewVersion , eg just simple version , then I get an error: 但是,如果我将变量命名为viewVersion以外的其他viewVersion (例如,简单version ,则会收到错误消息:

Neither the property "viewVersion" nor one of the methods "getViewVersion()", "isViewVersion()", "hasViewVersion()", "__get()" exist and have public access in class "Gutensite\CmsBundle\Entity\View\View".

All the getters and setters are created through php app/console doctrine:generate:entities but they are for getVersion() and not getViewVersion() . 所有的getter和setter都是通过php app/console doctrine:generate:entities但它们用于getVersion()而不是getViewVersion()

Question: So, is there some unspoken rule that associated entities MUST be named the same as their class name? 问题:那么,是否存在一些潜规则,即必须将关联实体的名称与其类名称相同?

Entity Definition 实体定义

/**
* @ORM\Entity
* @ORM\Table(name="view")
* @ORM\Entity(repositoryClass="Gutensite\CmsBundle\Entity\View\ViewRepository")
*/
class View extends Entity\Base {

    /**
    * @ORM\OneToOne(targetEntity="\Gutensite\CmsBundle\Entity\View\ViewVersion", inversedBy="view", cascade={"persist", "remove"}, orphanRemoval=true)
    * @ORM\JoinColumn(name="versionId", referencedColumnName="id")
    */
    protected $version;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $versionId = NULL;
}

FYI, the variables for associated entities can be whatever you want. 仅供参考,关联实体的变量可以是您想要的任何变量。

This was caused by a predefined formType still referencing "viewVersion". 这是由仍引用“ viewVersion”的预定义formType引起的。 The first variable in a form $builder->add() is a reference to the specific variable in the entity. $ builder-> add()形式的第一个变量是对实体中特定变量的引用。 I had viewVersion listed there still, and when I audited my code, I assumed it was just a generic reference (without any requirement) or possibly a reference to the Entity class, so I didn't change it: 我在那里仍然列出了viewVersion ,当我审核代码时,我认为它只是一个通用引用(没有任何要求)或可能是对Entity类的引用,所以我没有更改它:

$builder->add('viewVersion', new ViewVersionType(), array(
    'label' => false
));

The SOLUTION to this problem was to change viewVersion to version so that it references an actual variable on the entity. 解决此问题的方法是将viewVersion更改为version以便它引用实体上的实际变量。 Obviously... 明显...

$builder->add('version', new ViewVersionType(), array(
    'label' => false
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM