1

I'm trying to do mapping associations in symfony2 but I'm having some problems. Here's my code:

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

My question is the following

When I run the command: doctrine:schema:validate

why am I getting the following error:

[Mapping] FAIL - The entity-class 'OSC\UserBundle\Entity\User' mapping is invalid: * The association OSC\UserBundle\Entity\User#children refers to the owning side field OSC\UserBundle\Entity\Child#parent which does not exist.

I'm feeling this is linked to parent not being defined in Child. However, when I run doctrine:generate:entities OSC parent is not added....

1 Answer 1

6

Oh my god, it was so simple, you have to write manyToOne, not ManyToOne...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.