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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度