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();
}
}
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.
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.
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.
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:
Try to change fetch mode to EAGER.
@ORM\ManyToOne(targetEntity="****", fetch="EAGER").
It worked for me.
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 :)
//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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With