0

I have an issue with a ManyToOne - OneToMany relation with Doctrine 2 x Symfony 3..

I created 2 Entities (let's name them IPAddress and VPS), one VPS can have many ips and many ips can belong to a VPS

My IPAddress Entity:

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

My VPS Entity :

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

When I try to get the IPs through my VPS like this :

$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"

May someone have any idea and could help me with that issue? I'm searching since a day and can't find what I'm doing wrong..

Thanks in advance! :)

Unyxos
  • 77
  • 1
  • 11
  • at first you should assign the result of getIps() to some variable like this: `$ips = $server->getIps();` – Pavel Alazankin Feb 06 '18 at 11:52
  • I already tried to assign and then access my properties and it's not working too, it's weird, that's not the first time I'm working with that relation and I never had that issue before, I double checked and I don't see what's wrong.. – Unyxos Feb 06 '18 at 12:11
  • What did you try that is "not working too"? It is expected that the `ips` collection won't be populated unless you try to access it or explicitly join it when retrieving your `VPS` entity. What did you use to "access" the properties? – Alan T. Feb 06 '18 at 13:46

1 Answers1

0

Check out the example here. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional

Doctrine 2 can't use nullable=false in manyToOne relation?

Cesur APAYDIN
  • 806
  • 3
  • 11
  • 24
  • Hi! I already checked that and it don't refer to my issue haha, that's why i'm so desesperate right now – Unyxos Feb 06 '18 at 12:12