WellBloud
WellBloud

Reputation: 977

Doctrine 2 get items from related entities

I have an entity with ManyToOne, OneToOne relations. I have another entity, which is linked to the first one and what I want to do is to get information about the first entity as well as all other entities, which are related to it.

For some reason when I make the call, it returns NULL in the related entity.

Code:

<?php

namespace App\Model\Entities;

use Doctrine\ORM\Mapping as ORM,
    Doctrine\Common\Collections\ArrayCollection;
use Kdyby\Doctrine\Entities\BaseEntity;

/**
 * Doctrine entity for resorts in destinations
 * @package App\Model\Entities
 * @ORM\Entity
 */
class Resort extends BaseEntity
{

    /**
     * autoincremented resort id
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * resort name
     * @ORM\Column(type="string")
     */
    protected $title;

    /**
     * @ORM\OneToOne(targetEntity="ResortProperties", mappedBy="resort", cascade={"persist"})
     */
    private $properties;

    public function __construct()
    {
        $this->properties = new ArrayCollection();
    }

    /**
     * HERE I WANT TO GET DATA FROM ANOTHER ENTITY
     * @return type
     */
    public function getProperties()
    {
        return $this->properties;
    }

}

And the related entity is this:

<?php

namespace App\Model\Entities;

use Doctrine\ORM\Mapping as ORM,
    Doctrine\Common\Collections\ArrayCollection;
use Kdyby\Doctrine\Entities\BaseEntity;

/**
 * Doctrine entity for resort properties
 * @package App\Model\Entities
 * @ORM\Entity
 */
class ResortProperties extends BaseEntity
{

    /**
     * autoincremented id
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * resort id
     * @ORM\OneToOne(targetEntity="Resort", inversedBy="Resort", cascade={"persist", "remove"})
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    protected $resort;

    /**
     * parameter1
     * @ORM\Column(type="string")
     */
    protected $parameter1;
}

I expected, that when I call $repository->findAll(); I would get all Resort entities and in Resort->properties would be joined ResortProperty entity, but it is NULL.

I don't know what I did wrong, can someone point out my mistake? Thanks

Upvotes: 0

Views: 1996

Answers (2)

WellBloud
WellBloud

Reputation: 977

In ResortProperties entity I had to make this change:

class ResortProperties extends BaseEntity
{

    /**
     * resort id
     * @ORM\OneToOne(targetEntity="Resort", inversedBy="properties", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="resort_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $resort;
}

And in Resort entity I had to change it like this:

class Resort extends BaseEntity
{
    /**
     * @ORM\OneToOne(targetEntity="ResortProperties", mappedBy="resort", cascade={"persist"})
     */
    private $properties;

    public function __construct()
    {
        parent::__construct();
        $this->properties = new ArrayCollection();
    }
}

Now it's working properly, hope this will help others

Upvotes: 0

Julian Schachermeier
Julian Schachermeier

Reputation: 26

I assume the problem is the JoinColumn-Annotation. The Entity with the Foreign-key (ResortProperties) needs to know, how to join the two tables. The "inversedBy"-Statement on the Resort-Entity then uses this rule.

It should look something like the following in your ResortProperties-Entity:

    /**
     * resort id
     * @ORM\OneToOne(targetEntity="Resort", inversedBy="Resort", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="resort_id", referencedColumnName="id")
     */
    protected $resort;

"resort_id" is the name of your foreign-key-column.

Hope this helps.

Upvotes: 1

Related Questions