symfony2 doctrine onetoone complete example

六月ゝ 毕业季﹏ 提交于 2020-01-04 04:43:32

问题


i try make onetoone relation from example - http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

this is second try , first is here symfony 2 doctrine relation onetoone

Adres

<?php
/**
 * Created by PhpStorm.
 * User: grek
 * Date: 18.12.13
 * Time: 16:33
 */

namespace Miejsce\ObiektyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Adres {

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


    /**
     * @ORM\Column(type="string", length=64, nullable=true)
     */
    public $street;


    /**
     * @ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\User") */
    private $user;




}

User

    <?php
/**
 * Created by PhpStorm.
 * User: grek
 * Date: 18.12.13
 * Time: 16:33
 */

namespace Miejsce\ObiektyBundle\Entity;


use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 */
class User {


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


    /**
     * @ORM\Column(type="string", length=64, nullable=true)
     */
    public $name;



    /**
     * @ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\Adres")
     */
    private $adres;


}

and have : php app/console doctrine:schema:update --force

php app/console doctrine:schema:update --force

[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "@Doctrine\ORM\Mapping" in property Miejsce\ObiektyBundle\Entity\Adres::$user does not e
xist, or could not be auto-loaded.
So where i can have error ?

  • error was in syntax - i have @ORM/OneToOne but need @ORM\OneToOne now work fine ! :)

回答1:


I'm not sure if I understand exactly what you are asking, but your annotations are not setup correctly.

/**
 * @Entity

should become

/**
 * @ORM\Entity

And

/**
 * @Id @Column...

should become

/**
 * @ORM\Id
 * @ORM\Column...

Same for @OneToOne should be @ORM\OneToOne

Basically you are not prefixing your annotations correctly. You have use Doctrine\ORM\Mapping as ORM; but you aren't using it properly. Prefix your annotations and that will get you going.




回答2:


The answer is in the error message. Look at what is different between your two classes.

/**
 * @Entity
 */

Vs:

/**
 * @ORM\Entity
 */

So update the one that is giving you the error.

edit

When you import Doctrine's annotations with use Doctrine\ORM\Mapping as ORM;, you'll need to start all those annotations with @ORM\. The annotation-reader will know that @ORM\Entity will actually mean @Doctrine\ORM\Mapping\Entity, which is the class that defines that annotation.



来源:https://stackoverflow.com/questions/20663942/symfony2-doctrine-onetoone-complete-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!