我在Symfony(Doctrine)中创建两个表之间的关系时遇到问题。 我正在创建地址簿,我有Person Entity,我想要有关系机智地址实体与关系OneToMany(一个人有很多地址)。 我跑的时候:
php app/console doctrine:generate:entities AdressBookBundle/Entiti/Address
我收到了错误:
[RuntimeException]
Namespace "AdressBookBundle\Entiti\Address" does not contain any mapped entities.
下面我将代码放在两个实体中:
人:
<?php
namespace AdressBookBundle\Entity;
use AdressBookBundle\Entity\Address;
use Doctrine\ORM\Mapping as ORM;
/**
* Person
*
* @ORM\Table(name="person")
* @ORM\Entity(repositoryClass="AdressBookBundle\Repository\PersonRepository")
*/
class Person
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="surname", type="string", length=255)
*/
private $surname;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=500)
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="Address", mappedBy="person")
*/
private $address;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Person
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set surname
*
* @param string $surname
* @return Person
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* @return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* Set description
*
* @param string $description
* @return Person
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Constructor
*/
public function __construct()
{
$this->address = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add address
*
* @param \AdressBookBundle\Entity\Address $address
* @return Person
*/
public function addAddress(\AdressBookBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Remove address
*
* @param \AdressBookBundle\Entity\Address $address
*/
public function removeAddress(\AdressBookBundle\Entity\Address $address)
{
$this->address->removeElement($address);
}
/**
* Get address
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAddress()
{
return $this->address;
}
}
地址:
<?php
namespace AdressBookBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AdressBookBundle\Entity\Person;
/**
* Address
*
* @ORM\Table(name="address")
* @ORM\Entity(repositoryClass="AdressBookBundle\Repository\AddressRepository")
*/
class Address
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="city", type="text")
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="street", type="text")
*/
private $street;
/**
* @var int
*
* @ORM\Column(name="house_number", type="integer")
*/
private $houseNumber;
/**
* @var int
*
* @ORM\Column(name="house_unit_number", type="integer")
*/
private $houseUnitNumber;
/**
* $@ORM\ManyToOne(targetEntity="Person", inversedBy="address")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
*/
private $person;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set city
*
* @param string $city
* @return Address
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set street
*
* @param string $street
* @return Address
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* Get street
*
* @return string
*/
public function getStreet()
{
return $this->street;
}
/**
* Set houseNumber
*
* @param integer $houseNumber
* @return Address
*/
public function setHouseNumber($houseNumber)
{
$this->houseNumber = $houseNumber;
return $this;
}
/**
* Get houseNumber
*
* @return integer
*/
public function getHouseNumber()
{
return $this->houseNumber;
}
/**
* Set houseUnitNumber
*
* @param integer $houseUnitNumber
* @return Address
*/
public function setHouseUnitNumber($houseUnitNumber)
{
$this->houseUnitNumber = $houseUnitNumber;
return $this;
}
/**
* Get houseUnitNumber
*
* @return integer
*/
public function getHouseUnitNumber()
{
return $this->houseUnitNumber;
}
}
你能解释一下它是如何起作用的吗? 据我所知,我通过以下方式建立关系:
/**
* $@ORM\ManyToOne(targetEntity="Person", inversedBy="address")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
*/
并且neew列person_id
正在地址表中创建。我是对的吗?
答案 0 :(得分:0)
注释中有错误:
$@ORM\ManyToOne(targetEntity="Person", inversedBy="address")
应该是
@ORM\ManyToOne(targetEntity="Person", inversedBy="address")
删除 $ 以使其正常工作。
答案 1 :(得分:0)
从@ORM \ ManyToOne(targetEntity =“Client”)中删除“$”。
@ORM \多对一(targetEntity = “客户”)。删除“$”