0

I have a View entity that references an associated entity called ViewVersion. But if I name the variable anything other than viewVersion, e.g. just simple version, then I get an error:

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().

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;
}
Chadwick Meyer
  • 7,041
  • 7
  • 44
  • 65
  • Your class seems to be well. What's Entity\Base? Is it possible that a property viewVersion is defined in that class and the problem occurs for this reason? – Airam May 06 '14 at 23:31
  • The only reference to ViewVersion are to the entity itself, e.g. public function setVersion(\Gutensite\CmsBundle\Entity\View\ViewVersion $version = null) {} – Chadwick Meyer May 06 '14 at 23:46
  • So as far as you know, there is not a requirement that the variable be named the same as the associated entity class? It seems to be done in the examples I've seen, sort of, although on oneToMany examples the variable might be $products when referencing the product entity. So it's not exact. – Chadwick Meyer May 06 '14 at 23:48
  • Entity/Base is a mappedSuperClass with basic field definitions like $id, $time, $timeMod. I am not sure if that's the best way to do that but it seemed like it (I'm new to Symfony, obviously). I did a code audit and I'm 99% certain that there are no references to any function like *viewVersion that are being called. It seems to be internal... One thing that may complicate things is in a related question I asked: http://stackoverflow.com/questions/23506362/symfony-association-mapping-onetoone-and-onetomany-to-same-entity – Chadwick Meyer May 06 '14 at 23:58
  • Once you rename the viewVersion to version please make sure you have removed auto created getter/setter method of viewVersion class. Symfony entity generator command only write the code at the end of the file. they do not change any thing which is already created previously in file. – Kapil May 07 '14 at 11:40
  • Correct, thanks. Fortunately I did remove all those, but no success. I'm going to create some simple test entities and see how it works. It does seem most likely that there is some reference somewhere in my app to ViewVersion still, despite a thorough search. – Chadwick Meyer May 07 '14 at 16:21
  • @Airam I found the problem (see answer). Thanks for the ideas and reassurance that the problem was in the code not in a naming convention. – Chadwick Meyer May 08 '14 at 00:13

1 Answers1

0

FYI, the variables for associated entities can be whatever you want.

This was caused by a predefined formType still referencing "viewVersion". The first variable in a form $builder->add() is a reference to the specific variable in the entity. 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:

$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. Obviously...

$builder->add('version', new ViewVersionType(), array(
    'label' => false
));
Chadwick Meyer
  • 7,041
  • 7
  • 44
  • 65