duanque3125 2016-05-29 16:55
浏览 97
已采纳

OneToMany ManyToOne DoctrineORM错误

I'm newbie with PHP. I started work with symfony but i have this problem

/**
 * @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:

Symfony\Component\Debug\Exception\ContextErrorException: Notice: Undefined index: name at n/a

and when i run 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. *The association AppBundle\UserBundle\Entity\User#carList refers to the owning side field Appbundle\CarBundle\Entity\Car#name which does not exist

I have no idea whats going on, can you help me?

  • 写回答

1条回答 默认 最新

  • dongzouxigu12345 2016-05-29 17:44
    关注

    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)

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?