Jazi
Jazi

Reputation: 6732

Doctrine - Entity loaded without existing associations

I have two entity types: \Model\News and \Model\News\Category.

\Model\News: (without couple of fields)

namespace Model;

/**
 * @Entity
 * @Table(name="news")
 */
class News extends \Framework\Model {

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

    /**
     * @ManyToOne(targetEntity="\Model\User", inversedBy="news")
     */
    protected $author;

    /**
     * @ManyToOne(targetEntity="\Model\News\Category", inversedBy="news")
     */
    protected $category;
}

\Model\News\Category:

namespace Model\News;

/**
 * @Entity
 * @Table(name="news_category")
 */
class Category extends \Framework\Model {

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

    /**
     * @Column(type="string", length=50, unique=TRUE)
     */
    protected $name;

    /**
     * @OneToMany(targetEntity="\Model\News", mappedBy="category")
     */
    protected $news;

    /**
     * Constructor
     */
    public function __construct() {
        parent::__construct();

        $this->news = new \Doctrine\Common\Collections\ArrayCollection;
    }
}

Table data from \Model\News:

 id | category_id |  author_id
-------------------------------     ...
 4  |      1      |     NULL

Table data from \Model\News\Category:

 id |   name 
---------------
 1  | General 
---------------
 2  | Other

While I'm loading News type Entity with this particular code and doing dump with \Kint class:

$sId     = '4';
$sModel  = 'Model\News';
$oObject = $entity_manager->find($sModel, $sId);

d($oObject);

It returns me this:

Image 1

My question is, why category property from $oObject variable has NULL values despite the fact that the category with id = 1 exists in database?


UPDATE:

After above code, I want to load this category (with ID=1) separately. Same thing. But... when I'm loading a category with other ID (for example, 2) it's loading with no problems:

$oObject2 = $entity_manager->('\Model\News\Category', '2');

I have no idea what to do now...

Upvotes: 0

Views: 82

Answers (2)

lisa
lisa

Reputation: 579

If you know that the category entity will be accessed almost each time you load news, you might want to eager load it (force doctrine to load it when News is loaded instead of when a Category property is called).

In order to do so, just add the fetch="EAGER" annotation on your association

/**
 * @ManyToOne(targetEntity="\Model\News\Category", inversedBy="news", fetch="EAGER")
 */
protected $category;

Upvotes: 3

Jazi
Jazi

Reputation: 6732

So... I finally came to solve the problem thanks to @asm89 from #doctrine IRC chat in freenode server.

Doctrine is creating a proxy class which uses "lazy loading". Data is loaded only when one of getters is used.

So, after using $oObject->getCategory()->getName(), my category object available from $oObject->getCategory() was filled up with proper data.

Upvotes: 1

Related Questions