Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine2 association is not initialized

I have a Class like the following:

/** @Entity **/
class orgGroup{

    //id and stuff...

    /**
     * @Column(type="string")
     **/
    private $name;

    /**
     * @Column(type="string", nullable=true)
     **/
    private $description;

    /**
     * @ManyToOne(targetEntity="orgGroupType", inversedBy="_orgGroups")
     * @JoinColumn(name="_orgGroupType")
     **/

    private $_orgGroupType;

    //...
}

But when i load this Object from my database via

$groups = $em->getRepository("orgGroup")->findAll();

I just get the name correctly but not the _orgGroupType... and i dont know why... OrgGroup is the owner of orgGroupType and its just ONE object and not an array. My Webservice always just says:

{"error":[],"warning":[],"message":[],"data":[{"name":"AdministratorGroup","description":null,"_orgGroupType":{"__ isInitialized __":false}}]}

The result is:

"name":"AdministratorGroup",
"description":null,
"_orgGroupType":{"__ isInitialized __":false}

but should be:

"name":"AdministratorGroup",
"description":"some description",
"_orgGroupType":{name:"test"}

So there are 2 errors... and I have no idea why. All the data is set correctly in the database.

Any Ideas?

EDIT: Here's the missing code of my orgGroupType -entity

/** @Entity **/
class orgGroupType {
    /**
     * @OneToMany(targetEntity="orgGroup", mappedBy="_orgGroupType")
     **/
    private $_orgGroups;

    public function __construct()
    {
        $this->_orgGroups = new ArrayCollection();
    }
}
like image 443
Laokoon Avatar asked Nov 17 '12 14:11

Laokoon


People also ask

What is the event system in doctrine 2?

Doctrine 2 features a lightweight event system that is part of the Common package. Doctrine uses it to dispatch system events, mainly lifecycle events . You can also use it for your own custom events. 9.1. The Event System ¶ The event system is controlled by the EventManager. It is the central point of Doctrine’s event listener system.

Does doctrine 2 work with lower versions of PHP?

Some of the code will not work with lower versions. Doctrine 2 is an object-relational mapper (ORM) for PHP 5.4+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system.

Does doctrine 2 have anything to do with persistence?

This doesn’t mean persistence is downplayed by Doctrine 2, however it is our belief that there are considerable benefits for object-oriented programming if persistence and entities are kept separated. Entities are PHP Objects that can be identified over many requests by a unique identifier or primary key.

What is EntityManager in doctrine 2?

Doctrine’s public interface is the EntityManager, it provides the access point to the complete lifecycle management of your entities and transforms entities from and back to persistence. You have to configure and create it to use your entities with Doctrine 2. I will show the configuration steps and then discuss them step by step:


4 Answers

Try to change fetch mode to EAGER.

@ORM\ManyToOne(targetEntity="****", fetch="EAGER"). 

It worked for me.

like image 50
Praveen Avatar answered Oct 22 '22 23:10

Praveen


This looks to me like a lazy-loading-issue. How do you get the data from the object into the Webservice answer?

Doctrine2 is lazy-loading if you don't configure something else, that means your $groups = $em->getRepository("orgGroup")->findAll(); won't return real orgGroup objects, but Proxy objects (Doctrine Documentation).

That means a $group object won't have it's description or orgGroupType value until you call $group->getDescription() or $group->getOrgGroupType() (then Doctrine loads them automatically), so you need to do that before writing the data into the JSON-response for the webservice. It won't work if you somehow loop through the object properties without using the getter methods.

I hope that was the problem :)

like image 23
Manuel Avatar answered Oct 22 '22 23:10

Manuel


//Group.php ...

public function addUser(User $user): self
{
    if (!$this->users->contains($user)) {
        $this->users[] = $user;
        $user->addJoinedGroup($this);     /** VERY IMPORTANT **/
    }
    return $this;
}

Same in User.php Without it, it didn't do anything in my database

like image 31
TNP Avatar answered Oct 22 '22 23:10

TNP


Be sure to initialize the orgGroups collection in the orgGroupType entity

/**
 * @OneToMany(targetEntity="orgGroup", mappedBy="_orgGroupType")
 */
protected $orgGroups ;

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

You might need to include the following in the Entity

use Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
like image 36
Pankrates Avatar answered Oct 22 '22 22:10

Pankrates