简体   繁体   English

一对多多对一原则ORM错误

[英]OneToMany ManyToOne DoctrineORM Error

I'm newbie with PHP. 我是PHP的新手。 I started work with symfony but i have this problem 我从symfony开始工作,但是有这个问题

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
 * @param \Doctrine\Common\Collections\Collection $carList
 * @ORM\OneToMany(targetEntity="AppBundle\CarBundle\Entity\Car", mappedBy="name", cascade={"persist"})
 */
     private $carList;

//getters and setters

} }

     *
 * @ORM\Entity(repositoryClass="AppBundle\CarBundle\Repository\Entity\CarRepository")
 * @ORM\Table(name="car")
 */
class Car
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     *
     */
    protected $id;



    /**
 * @ORM\Column(type="string", length=100)
 * @ORM\ManyToOne(targetEntity="AppBundle\UserBundle\Entity\User" , inversedBy="carList")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")

 */     
    private $name;

//getters and setters

}

The stacktrace says: stacktrace说:

Symfony\\Component\\Debug\\Exception\\ContextErrorException: Notice: Undefined index: name at n/a Symfony \\ Component \\ Debug \\ Exception \\ ContextErrorException:注意:未定义的索引:名称不适用

and when i run php bin/console doctrine:schema:validate 当我运行php bin / console doctrine:schema:validate时

[Mapping] FAIL - The entity-class 'AppBundle\\UserBundle\\Entity\\User' mapping is invalid: * The association AppBundle\\UserBundle\\Entity\\User#carList refers to the owning side field AppBundle\\CarBundle\\Entity\\Car#name which is not defined as association, but as field. [映射]失败-实体类'AppBundle \\ UserBundle \\ Entity \\ User'映射无效:*关联AppBundle \\ UserBundle \\ Entity \\ User#carList引用拥有方字段AppBundle \\ CarBundle \\ Entity \\ Car#name,未定义为关联,而是字段。 *The association AppBundle\\UserBundle\\Entity\\User#carList refers to the owning side field Appbundle\\CarBundle\\Entity\\Car#name which does not exist *关联AppBundle \\ UserBundle \\ Entity \\ User#carList指的是不存在的拥有方字段Appbundle \\ CarBundle \\ Entity \\ Car#name

I have no idea whats going on, can you help me? 我不知道发生了什么事,您能帮我吗?

You are mixing up association names with column names. 您正在将关联名称与列名称混合在一起。 When you create an association you don't need to manually add the columns for that association, doctrine will work that out for you. 创建关联时,无需手动添加该关联的列,该学说将为您解决。

This code (in the Car class) says that the $name field is a normal text column in the car table, which of course is wrong * @ORM\\Column(name="name",type="string", length=100) 这段代码(在Car类中)说,$ name字段是car表中的普通文本列,这当然是错误的* @ORM\\Column(name="name",type="string", length=100)

What you're describing is that one user can own many cars, and many cars can belong to one user. 您要描述的是一个用户可以拥有多辆汽车,而许多汽车可以属于一个用户。 I'd then call the associations owner and cars, but you are of course free to call them whatever you want. 然后,我会打电话给协会老板和汽车,但是您当然可以随意叫它们。 Note that you do not need to define the join columns. 请注意,您不需要定义联接列。

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @param \Doctrine\Common\Collections\Collection $cars
     * @ORM\OneToMany(targetEntity="AppBundle\CarBundle\Entity\Car", mappedBy="owner", cascade={"persist"})
     */
    private $cars;

    public function __construct()
    {
       $this->cars = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //getters and setters
}

/**
 *
 * @ORM\Entity(repositoryClass="AppBundle\CarBundle\Repository\Entity\CarRepository")
 * @ORM\Table(name="car")
 */
class Car
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\UserBundle\Entity\User" , inversedBy="cars")
     */     
    private $owner;

//getters and setters

}

Read more: Doctrine association mapping 阅读更多: 教义关联映射

Hope it makes sense :) 希望有道理:)

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

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