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;
    }
}

私の質問は次のとおりです

コマンドを実行すると: doctrine:schema:validate

次のエラーが表示されるのはなぜですか。

[マッピング] 失敗 - エンティティ クラス 'OSC\UserBundle\Entity\User' マッピングが無効です: * アソシエーション OSC\UserBundle\Entity\User#children は、所有側フィールド OSC\UserBundle\Entity\Child#parent を参照します。存在しません。

これは、親が子で定義されていないことに関連していると感じています。ただし、doctrine:generate:entities OSC を実行すると親が追加されません....

4

1 に答える 1

6

なんてこった、とても簡単だったので、ManyToOneではなくmanyToOneと書く必要があります...

于 2013-05-27T13:45:50.483 に答える