1

我正在尝试在 symfony2 中进行映射关联,但我遇到了一些问题。这是我的代码:

OSC\UserBundle\Resources\doctrine\Child.orm.yml

OSC\UserBundle\Entity\Child:
    type: entity
    table: null
    repositoryClass: OSC\UserBundle\Entity\ChildRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        firstName:
            type: string
            length: 255
        lastName:
            type: string
            length: 255
        dateOfBirth:
            type: datetime
            column: dateOfBirth
        isPlayer:
            type: boolean
            default: false
        isCoach:
            type: boolean
            default: false

    ManyToOne:
        parent:
            targetEntity: User
            inversedBy: children

    lifecycleCallbacks: {  }

OSC\UserBundle\Entity\Child.php

<?php

namespace OSC\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Child
 */
class Child
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $firstName;

    /**
     * @var string
     */
    private $lastName;

    /**
     * @var \DateTime
     */
    private $dateOfBirth;


     protected $isPlayer;

     protected $isCoach;

     protected $player;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set firstName
     *
     * @param string $firstName
     * @return Child
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return Child
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Set dateOfBirth
     *
     * @param \DateTime $dateOfBirth
     * @return Child
     */
    public function setDateOfBirth($dateOfBirth)
    {
        $this->dateOfBirth = $dateOfBirth;

        return $this;
    }

    /**
     * Get dateOfBirth
     *
     * @return \DateTime 
     */
    public function getDateOfBirth()
    {
        return $this->dateOfBirth;
    }


    /**
     * Set isPlayer
     *
     * @param boolean $isPlayer
     * @return Child
     */
    public function setIsPlayer($isPlayer)
    {
        $this->isPlayer = $isPlayer;

        return $this;
    }

    /**
     * Get isPlayer
     *
     * @return boolean 
     */
    public function getIsPlayer()
    {
        return $this->isPlayer;
    }

    /**
     * Set isCoach
     *
     * @param boolean $isCoach
     * @return Child
     */
    public function setIsCoach($isCoach)
    {
        $this->isCoach = $isCoach;

        return $this;
    }

    /**
     * Get isCoach
     *
     * @return boolean 
     */
    public function getIsCoach()
    {
        return $this->isCoach;
    }

}

OSC\UserBundle\Entity\User.orm.yml

OSC\UserBundle\Entity\User:
    type: entity
    table: null
    repositoryClass: OSC\UserBundle\Entity\UserRepository
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        lastName:
            type: string
            length: 255

        firstName:
            type: string
            length: 255

        streetNumber:
            type: string
            length: 255

        street:
            type: string
            length: 255

        province:
            type: string
            length: 255

        country:
            type: string
            length: 255

        homePhone:
            type: string
            length: 255

        mobilePhone:
            type: string
            length: 255
        isPlayer:
            type: boolean
            default: false
        isCoach:
            type: boolean
            default: false

        dateOfBirth:
            type: date
            column: dateOfBirth

    oneToMany:
        children:
            targetEntity: Child
            mappedBy: parent

    lifecycleCallbacks: {  }

/

<?php

namespace OSC\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


class User extends BaseUser
{

    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic

    }

   /**
     *
     * @Assert\NotBlank(message="Please enter your last name.", groups={"Registration", "Profile"})
     * @Assert\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
     * @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
     */
     protected $lastName;

   /**    
     * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"})
     * @Assert\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
     * @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
     */
     protected $firstName;


     protected $streetNumber;

     protected $street;

     protected $province;

     protected $country;

     protected $postalCode;

     protected $homePhone;

     protected $mobilePhone;

     protected $dateOfBirth;

     protected $children;

     protected $isPlayer;

     protected $isCoach;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return User
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Set firstName
     *
     * @param string $firstName
     * @return User
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Set streetNumber
     *
     * @param string $streetNumber
     * @return User
     */
    public function setStreetNumber($streetNumber)
    {
        $this->streetNumber = $streetNumber;

        return $this;
    }

    /**
     * Get streetNumber
     *
     * @return string 
     */
    public function getStreetNumber()
    {
        return $this->streetNumber;
    }

    /**
     * Set street
     *
     * @param string $street
     * @return User
     */
    public function setStreet($street)
    {
        $this->street = $street;

        return $this;
    }

    /**
     * Get street
     *
     * @return string 
     */
    public function getStreet()
    {
        return $this->street;
    }

    /**
     * Set province
     *
     * @param string $province
     * @return User
     */
    public function setProvince($province)
    {
        $this->province = $province;

        return $this;
    }

    /**
     * Get province
     *
     * @return string 
     */
    public function getProvince()
    {
        return $this->province;
    }

    /**
     * Set country
     *
     * @param string $country
     * @return User
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return string 
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set homePhone
     *
     * @param string $homePhone
     * @return User
     */
    public function setHomePhone($homePhone)
    {
        $this->homePhone = $homePhone;

        return $this;
    }

    /**
     * Get homePhone
     *
     * @return string 
     */
    public function getHomePhone()
    {
        return $this->homePhone;
    }

    /**
     * Set mobilePhone
     *
     * @param string $mobilePhone
     * @return User
     */
    public function setMobilePhone($mobilePhone)
    {
        $this->mobilePhone = $mobilePhone;

        return $this;
    }

    /**
     * Get mobilePhone
     *
     * @return string 
     */
    public function getMobilePhone()
    {
        return $this->mobilePhone;
    }

    /**
     * Add children
     *
     * @param \OSC\UserBundle\Entity\Children $children
     * @return User
     */
    public function addChild(\OSC\UserBundle\Entity\Children $children)
    {
        $this->children[] = $children;

        return $this;
    }

    /**
     * Remove children
     *
     * @param \OSC\UserBundle\Entity\Children $children
     */
    public function removeChild(\OSC\UserBundle\Entity\Children $children)
    {
        $this->children->removeElement($children);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Set isPlayer
     *
     * @param boolean $isPlayer
     * @return User
     */
    public function setIsPlayer($isPlayer)
    {
        $this->isPlayer = $isPlayer;

        return $this;
    }

    /**
     * Get isPlayer
     *
     * @return boolean 
     */
    public function getIsPlayer()
    {
        return $this->isPlayer;
    }

    /**
     * Set isCoach
     *
     * @param boolean $isCoach
     * @return User
     */
    public function setIsCoach($isCoach)
    {
        $this->isCoach = $isCoach;

        return $this;
    }

    /**
     * Get isCoach
     *
     * @return boolean 
     */
    public function getIsCoach()
    {
        return $this->isCoach;
    }

    /**
     * Set dateOfBirth
     *
     * @param \DateTime $dateOfBirth
     * @return User
     */
    public function setDateOfBirth($dateOfBirth)
    {
        $this->dateOfBirth = $dateOfBirth;

        return $this;
    }

    /**
     * Get dateOfBirth
     *
     * @return \DateTime 
     */
    public function getDateOfBirth()
    {
        return $this->dateOfBirth;
    }
}

我的问题如下

当我运行命令时:教义:模式:验证

为什么我收到以下错误:

[映射] FAIL - 实体类 'OSC\UserBundle\Entity\User' 映射无效:* 关联 OSC\UserBundle\Entity\User#children 指的是拥有方字段 OSC\UserBundle\Entity\Child#parent不存在。

我觉得这与未在 Child 中定义的 parent 有关。但是,当我运行教义时:generate:entities OSC parent 没有添加....

4

1 回答 1

6

哦,我的上帝,这太简单了,你必须写 manyToOne,而不是 ManyToOne...

于 2013-05-27T13:45:50.483 回答