我遇到了与Doctrine 2 x Symfony 3的ManyToOne - OneToMany关系的问题。
我创建了2个实体(让他们将它们命名为IPAddress和VPS),一个VPS可以拥有多个ips,许多ips可以属于VPS
我的IPAddress实体:
class IPAddress
{
....
/**
* Many IPs have one server
*
* @ORM\ManyToOne(targetEntity="VPS", inversedBy="ips")
*/
protected $server;
/**
* @return mixed
*/
public function getServer()
{
return $this->server;
}
/**
* @param mixed $server
*/
public function setServer($server)
{
$this->server = $server;
}
....
}
我的VPS实体:
Class VPS
{
....
/**
* One server have many IPs
*
* @ORM\OneToMany(targetEntity="IPAddress", cascade={"persist", "remove"}, mappedBy="server")
*/
protected $ips;
public function __construct()
{
$this->ips = new ArrayCollection();
}
/**
* @return mixed
*/
public function getIps()
{
return $this->ips;
}
/**
* @param mixed $ips
*/
public function addIps(IPAddress $IPAddress)
{
$this->ips->add($IPAddress);
$IPAddress->setServer($this);
}
....
}
当我尝试通过我的VPS获取IP时:
$em = $this->getDoctrine()->getManager();
$serverRepo = $em->getRepository('AppBundle:VPS');
$server = $serverRepo->find(4);
$server->getIps();
//From there, there is no way to get my IPs, I only have a very big object without my IPs informations, only my columns' names marked as "null"
愿有人有任何想法,可以帮我解决这个问题吗?我从一天开始搜索,无法找到我做错的事情。
提前致谢! :)