Is there a better way to handle the Doctrine proxy object - php

I have two classes linked with a one-to-one relation.
class Client {
...
/**
* #ORM\OneToOne(targetEntity="ClientInfo")
* #ORM\JoinColumn(name="id", referencedColumnName="client_id")
*/
private $info;
...
public function doSomething() {
if (!$this->getInfo() instanceof ClientInfo) {
return false;
}
return $this->getInfo()->doSomething();
}
...
}
class ClientInfo {
...
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="Client")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
...
public function doSomething() {
return 'something';
}
...
}
Those classes are loaded with database content with Doctrine. It is working perfectly when there is data in the database. But if there is not ClientInfo data, I have a \Doctrine\ORM\EntityNotFoundException raised.
So I changed the doSomething() method to take this into account.
public function doSomething() {
if (!$this->getInfo() instanceof ClientInfo) {
return false;
}
try {
return $this->getInfo()->doSomething();
} catch (\Doctrine\ORM\EntityNotFoundException $e) {
return false;
}
}
But it does not feel right to me since it is tied with Doctrine. I am trying to modify my unit tests to add a mock of the proxy object but it does not feel right either.
Is there a better way of doing that?
EDIT 1
I followed Nico Kaag suggestion but it does not change anything.
My constructor in my Client class look like this:
public function __construct() {
$this->info = new ClientInfo();
}
If I do a var_dump of $this->info after retrieving my object with Doctrine, this is what I get.
object(Proxies\__CG__\MyBundle\Entity\ClientInfo)[444]
public '__initializer__' =>
object(Closure)[461]
public '__cloner__' =>
object(Closure)[462]
public '__isInitialized__' => boolean false
private 'client' (MyBundle\Entity\ClientInfo) => string '21055' (length=5)
...
EDIT 2
I finally changed what I have done. I removed the try..catch block and change the query to retrieve objects from database. Now I force the query to retrieve the ClientInfo object at the same time as the Client object.
This way, I can trust my test and if I forget to query both objects simultaneously, I will have an exception to remind it to me.

See I have made classes for you.
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class client
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Entities\client_info", inversedBy="client")
* #ORM\JoinColumn(name="client_info_id", referencedColumnName="id", unique=true)
*/
private $clientInfo;
}
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class client_info
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Entities\client", mappedBy="clientInfo")
*/
private $client;
}
Try this, you will not get such issue.
Also I have used bi-directional relation with cardinality one-to-one, parent connection 0:1*- (parent optional), please see the diagram.
Suggetion : Use ORM designer tool for designing and extracting entity classes.

Related

Inheritance relationship with interfaces in symfony/doctrine

I have entities like this :
Request.php (parent)
/**
* #ORM\Entity(repositoryClass=RequestRepository::class)
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({
* "requestA" = "RequestA",
* "requestB" = "RequestB"
* })
*/
abstract class Request
{
/*...*/
}
RequestA.php (child A)
/**
* #ORM\Entity(repositoryClass=DemenagementRepository::class)
*/
class RequestA extends Request implements AddressEntityInterface
{
/**
* #ORM\OneToOne(targetEntity=Address::class, inversedBy="request")
* #ORM\JoinColumn(nullable=false)
*/
private $address;
// +others...
}
RequestB.php (child B)
/**
* #ORM\Entity(repositoryClass=DemenagementRepository::class)
*/
class RequestB extends Request implements AddressEntityInterface
{
/**
* #ORM\OneToOne(targetEntity=Address::class, inversedBy="request")
* #ORM\JoinColumn(nullable=false)
*/
private $address;
// +others...
}
AddressEntityInterface.php
interface AddressEntityInterface
{
public function getAddress(): ?Address;
public function setAddress(Address $address): self;
}
Address.php
/**
* #ORM\Entity(repositoryClass=AddressRepository::class)
*/
class Address
{
/**
* #ORM\OneToOne(targetEntity=??????, mappedBy="address")
*/
private $request;
public function getRequest() { /* ... */ }
}
I want to use getRequest() revert relationship how can i do for make targetEntity dynamically ?
Thanks
I believe you need polymorphic relationships or as they like to call them from docrine 'Inheritance Mapping'
I leave you some sites to view on which you can find everything you need
Official Documentation (Doctrine)
Stackoverflow Practical example
Youtube tutorial
Stackoverflow, discussion <-
personally I advise you to read all this so you get a concrete idea of ​​what you need
I hope my little routing will help you.

Using data of Doctrine Collection from OneToMany association in JSON

I've seen many examples of how to set up a OneToMany association between Entities. However, I have not seen anything on how to output the data from an association. (such as converting to JSON or just having a clean array)
So, here is some sample code:
declare(strict_types=1);
namespace Banks\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html
*
* #ORM\Entity
* #ORM\Table(name="bank")
**/
class Banks implements \JsonSerializable
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="id", nullable=false)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* A Bank could have Many Branches
*
* #ORM\OneToMany(targetEntity="Branches\Entity\Branches", mappedBy="bank")
*
*/
protected $branches;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $name;
/**
*
* #return array|mixed
*/
public function jsonSerialize()
{
return [
'id' => $this->id,
'name' => $this->name,
'branches' => $this->getBranches()
];
}
public function __construct()
{
$this->branches = new ArrayCollection();
}
public function getBranches(): Collection
{
return $this->branches;
}
// ... Other getter/setters removed
}
Then we also have the Branches Entity:
declare(strict_types=1);
namespace Branches\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html
*
* #ORM\Entity
* #ORM\Table(name="branches")
**/
class Branches implements \JsonSerializable
{
/**
* #ORM\Id
* #ORM\Column(type="integer", nullable=false)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* A Branch has one Bank
*
* #ORM\ManyToOne(targetEntity="Banks\Entity\Banks", inversedBy="branches")
* #ORM\JoinColumn(name="bank_id", referencedColumnName="id")
*/
protected $bank;
/**
* #ORM\Column(type="integer", nullable=false)
*/
protected $bank_id;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $name;
/**
*
* #return array|mixed
*/
public function jsonSerialize()
{
return [
'id' => $this->id,
'bank_id' => $this->bank_id,
'name' => $this->name,
'bank' => $this->getBank()
];
}
public function getBank()
{
return $this->bank;
}
// ... Other getter/setters removed
}
Querying both Entities work fine overall, with calls to $result->jsonSerialize(), then returning with return new JsonResponse($result) to get a JSON object. Though querying a Branch has the expected result, where I receive the Branch along with the associated Bank as part of the output, the query to Bank is not returning the associated Branches and instead only displays as "branches": {}
I know this is because $branches is a Collection, but how to output it in a way to be part of the resulting JSON object?
I've tried $this->branches->toArray(), but that results in an array of Objects that cannot be encoded to JSON, therefore, ending in an error.
NOTE: The contents (Object) of $this->getBranches() does contain the Branches as expected, which can be seen by $this->branches->count(). But how to reach them in such a way to allow JsonSerializable to create the JSON?
As requested, here is middleware code leaving up to Entity usage:
A factory is used to create what is needed by the Handler:
class BanksViewHandlerFactory
{
public function __invoke(ContainerInterface $container) : BanksViewHandler
{
$entityManager = $container->get(EntityManager::class);
$entityManager->getConfiguration()->addEntityNamespace('Banks', 'Banks\Entity');
$entityRepository = $entityManager->getRepository('Banks:Banks');
return new BanksViewHandler($entityManager, $entityRepository);
}
}
The Factory calls the Handler:
class BanksViewHandler implements RequestHandlerInterface
{
protected $entityManager;
protected $entityRepository;
public function __construct(
EntityManager $entityManager,
EntityRepository $entityRepository,
) {
$this->entityManager = $entityManager;
$this->entityRepository = $entityRepository;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$return = $this->entityRepository->find($request->getAttribute('id'));
$result['Result']['Banks'] = $return->jsonSerialize();
return new JsonResponse($result);
}
}
The handler returns the JSON.
It's important to note that, when implementing the \JsonSerializable interface, calling jsonSerialize() directly does not return JSON, and you do not call this method explicitly.
As stated in the docs:
Objects implementing JsonSerializable can customize their JSON representation when encoded with json_encode().
The intent of implementing this interface is to enforce the jsonSerialize() method, which is called internally when passing the object(s) to json_encode(); e.g:
$result = $banksRepository->find($id);
$json = json_encode($result);
Additionally, if you want to serialize the child Branch entities as well you need to:
Implement \JsonSerializable for this entity (which you have done)
Doctrine will return these Branches as an ArrayCollection object, containing all child Branch objects. In order to ensure that json_encode() encodes these to JSON properly you need to convert the ArrayCollection to an array using toArray().
To illustrate - (as you pointed out you also implemented this):
public function jsonSerialize()
{
return [
'id' => $this->id,
'name' => $this->name,
'branches' => $this->getBranches()->toArray(), // <--
];
}
This should serialise your Bank and associated Branch entities as expected. Hope this helps :)

Doctrine 2 OneToOne, load owning side only when neccessary

I have two entities - User and UserSettings. In User entity, I want to have UserSettings as an attribute. That would be OK, I would add a OneToOne relation but there's a problem - because UserSettings is an owning side of the relation, every time I load User entity, Doctrine has to load the UserSettings entity too.
Is there a way how to load User but not UserSettings?
I made maybe a weird solution - there's no relation between these entities and the settings are loaded by method of Facade. For example:
/**
* #ORM\Entity
*/
class User
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $name;
/** #var UserSettings */
private $settings;
public function __construct()
{
$this->settings = new UserSettings();
}
public function setSettings(UserSettings $settings)
{
$this->settings = $settings;
}
}
/**
* #ORM\Entity
*/
class UserSettings
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(name="user_id", type="integer")
*/
private $userId;
}
class UserFacade
{
/**
* #var EntityManager
*/
private $em; // is injected automatically by DI
public function loadSettings(User $user)
{
$settings = $this->em->getRepository("UserSettings")->findOneBy(array("userId" => $user->id));
$user->setSettings($settings);
}
}
$user = $em->find("User", 1);
// if I want user's settings
$userFacade->loadSettings($user); // now I can use $user->getSettings()->something;
Side note: UserFacade is a service class that manipulates with users' data like adding new user, editing, deleting etc. In my MVC application, controller classes communicate with Facades, not with EntityManager directly.
That's OK - settings are loaded only when I want to. However, there are two possible problems:
a) I don't think this is a clear way
b) When I want a list of users, I cannot JOIN a table where settings are, because entities are not associated, so I have to make an extra SQL for each user.
My question is - how to solve the problem with OneToOne relation? I don't have much experience with Doctrine, so it may be a stupid question - sorry for that.
Thanks!

Doctrine: Object of class User could not be converted to string

I keep getting this error with Doctrine:
PHP Catchable fatal error: Object of class User could not be converted to string in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 1337
In my system users can have many permissions in a One to Many relationship. I have set up a User and Permission entity. They look like this (I removed some annotations, getters and setters to reduce clutter):
class User {
/**
* #ORM\Column(name="user_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
public function getId()
{
return $this->id;
}
/**
* #ORM\OneToMany(targetEntity="Permission", mappedBy="user", cascade={"persist"})
*/
protected $permissions;
public function getPermissions()
{
return $this->permissions;
}
}
class Permission {
/**
* #ORM\Column(name="user_id", type="integer")
* #ORM\ManyToOne(targetEntity="User", inversedBy="permissions")
*/
protected $user;
public function getUser()
{
return $this->user;
}
public function setUser( $user )
{
$this->user = $user;
return $this;
}
}
The problem occurs when I add a new Permission to a User:
$permission = new Permission();
$user->getPermissions()->add( $permission );
$em->persist( $user );
$em->flush();
This is the last bit of my stack trace:
PHP 11. Doctrine\ORM\UnitOfWork->persist() vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:565
PHP 12. Doctrine\ORM\UnitOfWork->doPersist() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1555
PHP 13. Doctrine\ORM\UnitOfWork->cascadePersist() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1615
PHP 14. Doctrine\ORM\UnitOfWork->doPersist() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2169
PHP 15. Doctrine\ORM\UnitOfWork->persistNew() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1597
PHP 16. Doctrine\ORM\UnitOfWork->scheduleForInsert() doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:836
PHP 17. Doctrine\ORM\UnitOfWork->addToIdentityMap() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1157
PHP 18. implode() vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1337
Any insight would be greatly appreciated.
OK. I've got it working.
I haven't fully worked out the reason yet but when I add the following to my User entity it works:
class User {
public function __toString()
{
return strval( $this->getId() );
}
}
If I find out more I will post here.
Your solution gave me a clue of what is happening.
Even though you have the entities and the anotations, Doctrine is not being able to understand the relation between entities. When doctrine understands the relation between entities, it knows what methods to call (ie User::getId()) but otherwise, it tries to transform whatever you are sending to a scalar value that it can use to query the database. Thats why it is calling the __toString function of the User, and thats why if you return the id in toString, everything works from here.
This is ok, but its a patch, and probably you dont want to keep it if we can find a better solution, since it could be harder to maintain as your application grows.
What i can see, is that in Permissions you have:
/**
* #ORM\Column(name="user_id", type="integer")
* #ORM\ManyToOne(targetEntity="User", inversedBy="permissions")
*/
protected $user;
You should remove the #ORM\Column(type="integer")
About the join columns, it is not mandatory, but you have to be sure that the defauts, are what you want. As we can read here
Before we introduce all the association mappings in detail, you should
note that the #JoinColumn and #JoinTable definitions are usually
optional and have sensible default values. The defaults for a join
column in a one-to-one/many-to-one association is as follows:
name: "<fieldname>_id"
referencedColumnName: "id"
so, they will be the same as an explicit:
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="permissions", cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
protected $user;
So it is supposed to look for a column user_id in the Permissions table, and join it with the id column of the User table. We suppose that this is ok.
If this is true, then in your User, the id shouldnt be user_id, but id:
/**
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
Or if the column name is actually user_id, then the User class is ok, but you have to change the join column to #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
That much i can say. I cannot try it know, but i will be glad if you can give it a second.
I think there's a problem with the mapping of user property in permission entity. Try this one:
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="permissions")
* #JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
are you initializing the collection in your OneToMany side?
and also, the methods to add and remove from the collection?
class User {
/**
* #ORM\OneToMany(targetEntity="Permission", mappedBy="user", cascade={"persist"})
*/
protected $permissions;
public function getPermissions()
{
return $this->permissions;
}
public function __construct()
{
$this->permissions = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addPermissions (Permission $permissions)
{
$this->permissions[] = $permissions;
return $this;
}
public function removePermissions(Permission $permissions)
{
$this->permissions->removeElement($permissions);
}
//...

Removing entities from another entity

To make it simple, let's say I have two objects with one-to-many relation:
User --(1:n)--> Request
with User defined as
class User {
...
/** #OneToMany(targetEntity="Request", mappedBy="user", cascade={"all"}) */
private $request;
...
}
and Request defined as
class Request {
...
/** #ManyToOne(targetEntity="User", inversedBy="request", cascade={"persist"}) */
private $user;
...
}
Is it possible to create a method that removes all Requests associated with User from within User entity?
What I need is something like this:
class User {
....
public function removeAllMyRequests() {
foreach ($this->getAllMyRequests() as $req)
$this->em->remove($req);
}
....
}
But apparently I'm not supposed to invoke entity manager from within entity.
You can mark the association with "Orphan Removal":
/**
* #Entity
*/
class User
{
/**
* #OneToMany(
* targetEntity="Request",
* mappedBy="user",
* cascade={"all"},
* orphanRemoval=true
* )
*/
private $requests;
}
Any Request object removed from the User#requests collection will be marked for removal during the next EntityManager#flush() call.
To remove all items at once, you can simply use Doctrine\Common\Collections\Collection#clear():
public function removeAllMyRequests() {
$this->requests->clear();
}
I think you are looking for the "cascade" option : http://docs.doctrine-project.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations

Categories