How to join entites like they are tables in Doctrine 2? - php

The documentation on how to retrieve joined entites as one object is very sparse, and most Stack Overflow questions on the subject are many years old.
I have a symfony project up and running with a database schema fully mapped in Doctrine. In my controllers I am able to run these two queries one after the other and they work fine.
$page = $this->getDoctrine()
->getRepository('PageBundle:SitePages')
->findByprodpageid($id);
$matrices = $this->getDoctrine()
->getRepository('PageBundle:SiteMatrices')
->findByprodpageid($id);
however both of them contain the attribute prodpageid and I would like to join the two entities on this column and receive one object containing all column values from both tables.
I am building this on top of an existing database structure so anything to do with changing the database structure etc is out of the question.
I have added annotations in my entities to specify which columns should be joined, in a ManyToOne relationship. But how do I activate that relation and receive the joined object?
Thanks for any info on the subject.
EDIT: Here are my relationships from the entities
//Entities/SitePages
/**
* #var integer
*
* #ORM\Column(name="ProdPageID", type="smallint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #ORM\OneToMany(targetEntity="SiteMatrices")
* #ORM\JoinColumn(name="prodpageid", referencedColumnName="prodpageid")
*/
private $prodpageid;
//Entities/SiteMatrices
/**
* #var integer
*
* #ORM\Column(name="ProdPageID", type="smallint", nullable=false)
* #ORM\ManyToOne(targetEntity="SitePages")
* #ORM\JoinColumn(name="prodpageid", referencedColumnName="prodpageid")
*
*/
private $prodpageid;

You are saying that a Page has many Matrices. I will make some changes by your permission in mapping annotations:
/**
* Entities/SitePages
*
* #var integer
*
* #ORM\Column(name="ProdPageID", type="smallint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #ORM\OneToMany(targetEntity="SiteMatrices")
* #ORM\JoinColumn(name="prodpageid", referencedColumnName="prodpageid")
*/
private $matrices;
/**
* #return ArrayCollection
*/
public function getMatrices(){
return $this->matrices;
}
/**
* #param Entities/SiteMatrices[]
* #return $this
*/
public function setMatrices($matrices){
$this->matrices = $matrices;
return $this;
}
and
/**
* Entities/SiteMatrices
*
* #var integer
*
* #ORM\Column(name="ProdPageID", type="smallint", nullable=false)
* #ORM\ManyToOne(targetEntity="SitePages")
* #ORM\JoinColumn(name="prodpageid", referencedColumnName="prodpageid")
*
*/
private $page;
/**
* #return Entities/SitePages
*/
public function getPage(){
return $this->page;
}
/**
* #param Entities/SitePages
* #return $this
*/
public function setPage($page){
$this->page = $page;
return $this;
}
Now if you query the Pages with this DQL:
$pages = $this->getDoctrine()
->getRepository('PageBundle:SitePages')
->findByprodpageid($id);
Then you could get each page's matrices simply by traversing on matrices association:
foreach($pages as $page){
$matrices = $page->getMatrices(); // will give you an ArrayCollection of all matrices objects joined by prodpageid to this page.
}
Hope I did not get you wrong and it helps.

Related

There is a way to load roles from a specific table in Symfony?

I have an app that manages all of its users inside one entity (USER), implementing the UserInterface, as recommended.
Linked to it, I got the CUSTOMER and the EMPLOYEE entities: each one storing specific infos about the user.
You may ask why I'm doing like this: an EMPLOYEE can be a CUSTOMER in many WORKPLACES and may work in many WORKPLACES. In order to avoid repetition, the common info got centralized in USER and the attribute Roles in USER is always blank in db.
The goal here is to load my roles based on specific data stored in another entity (WORKPLACE) when an EMPLOYEE logs in.
My great question may live in the UserRepository: when symfony calls the repository looking for the user entered on the login form, doctrine delivers it with no roles. I put my roles and return the User to the Authenticator (the vanilla one, I havent touched it yet). After the end of every requery, Symfony checks for the user but then doctrine loads the ROLE_CLIENT (in other words - no privileges).
What I already tried and failed
Make another authenticator supporting the routes that need those specific rules (as seen here https://symfony.com/doc/current/security/guard_authentication.html )
Use the UserLoaderInterface inside UserRepository (as seen here https://symfony.com/doc/master/security/user_provider.html#using-a-custom-query-to-load-the-user )
Voters (I didnt see a way to code them to my needs)
The entities
class User implements UserInterface
{
/**
* #ORM\Id()
*/
private $cpf;
/**
* #ORM\Column
*
*/
private $email;
/**
* #ORM\Column
*/
private $nome;
/**
* #var string
*
* #ORM\Column
*/
private $telefone;
/**
* #var \DateTime|null
*
* #ORM\Column
*/
private $nascimento;
/**
* #var \DateTime|null
*
* #ORM\Column
*/
private $ultimoLogin;
/**
* #var string|null
*
* #ORM\Column
*/
private $endereco;
/**
* #var string|null
*
* #ORM\Column
*/
private $cidade;
/**
* #var string|null
*
* #ORM\Column
*/
private $uf;
/**
* #var int|null
*
* #ORM\Column
*/
private $cep;
/**
* #ORM\Column(name="Roles", type="json")
*/
private $roles = [];
/**
* #var string The hashed password
* #ORM\Column(name="password", type="string")
*/
private $password;
//vanilla getters and setters
Workplace entity
The booleans store the privileges i want to get
class LocalTrabalho
{
/**
* #var Configuracao
*
* #ORM\ManyToOne(targetEntity=Configuracao::class, inversedBy="localTrabalho")
* #ORM\JoinColumn(name="CNPJ", referencedColumnName="CNPJ", nullable=false)
* #ORM\Id
* the company unique code
*/
private $cnpj;
/**
* #var Funcionario
*
* #ORM\ManyToOne(targetEntity=Funcionario::class, inversedBy="localTrabalho")
* #ORM\JoinColumn(name="CPF_Funcionario", referencedColumnName="CPF", nullable=false)
* #ORM\Id
* the user-employee unique code
*/
private $cpfFuncionario;
/**
* #var bool
*
* #ORM\Column
* is this employee is active?
*/
private $ativo = 1;
/**
* #var bool
*
* #ORM\Column
*/
private $privilegioCaixa = 0;
/**
* #var bool
*
* #ORM\Column
*/
private $privilegioPrestador = 1;
/**
* #var bool
*
* #ORM\Column
*/
private $privilegioRecepcao = 0;
/**
* #var bool
*
* #ORM\Column
*/
private $privilegioAdministracao = 0;
Employee Entity
class Funcionario
{
/**
* #var int
*
* #ORM\Column
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $cpf;
/**
* #var string|null
*
* #ORM\Column
*/
private $ctps;
/**
* #var string|null
*
* #ORM\Column
*/
private $foto;
An example USER tuple from db
# CodPFis, email, password, Roles, Nome, Telefone, Nascimento, Ultimo_login, Endereco, Cidade, UF, CEP
'89038252099', 'sophiejenniferteresinhaoliveira__sophiejenniferteresinhaoliveira#grupomozue.com.br', '$argon2id$v=19$m=65536,t=4,p=1$WEn7b64I9744kRJICEpaLA$jcYLDvh2bZsZPakMDGsncpbfIZwR6lN0QcgJOOSerK0', NULL, 'João da Silva', '', NULL, NULL, NULL, NULL, NULL, NULL
My last resource is asking here, after some long and wasted work hours, what should I do (or what i did wrong)?
I've seen some other threads asking similar questions, but they're outdated (im using the version 5):
Dynamic roles in symfony
Symfony 2 - Loading roles from database
Symfony2 - Dynamic Role Management
How to update roles in security token in Symfony 4 without re-logging in
Symfony User Logout After Role Change
PS: Someone tried this approach (https://github.com/symfony/symfony/issues/12025#issuecomment-562004005) and got success in symfony5?
Well, turns out that the best solution for my case was manipulate the User Repository. Doctrine uses them to operate the db so I just coded my rules inside and let the framework do the rest.
The algorithm is something like the shown below:
Find the user using the provided $id
If he/she works somewhere, load the ROLE constants into the User entity
If he/she does not have a job, load the profile as a common user
public function find($id, $lockmode=null, $lockversion = null){
$us = $this->createQueryBuilder('u')
->andWhere('u.cpf = :cpf')
->setParameter('cpf', $id['cpf'])
->getQuery()->getOneOrNullResult();
$lt = $this->_em->createQuery(
'SELECT t
FROM App\Entity\LocalTrabalho t
WHERE t.cpfFuncionario = :cpf'
)
->setParameters(['cpf' => $us->getCpf()])
->getResult();
if (count($lt) > 0){
$regras = ['ROLE_FUNCIONARIO'];
if ($lt[0]->getPrivilegioCaixa()) $regras[] = 'ROLE_CAIXA';
if ($lt[0]->getPrivilegioPrestador()) $regras[] = 'ROLE_PRESTADOR';
if ($lt[0]->getPrivilegioRecepcao()) $regras[] = 'ROLE_RECEPCAO';
if ($lt[0]->getPrivilegioAdministracao())
{
$regras = ['ROLE_ADMIN'];
}
$us->setRoles($regras);
}
return $us;
}

Symfony2 doctrine:generate:entities wrong methods name

My entity class is:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\TipiVendita", inversedBy="idAgente")
* #ORM\JoinTable(name="tipi_vendita_agenti",
* joinColumns={
* #ORM\JoinColumn(name="id_agente", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="id_tipo_vendita", referencedColumnName="id")
* }
* )
*/
private $idTipoVendita;
/**
* Constructor
*/
public function __construct()
{
$this->idTipoVendita = new \Doctrine\Common\Collections\ArrayCollection();
}
and when i use the command 'doctrine:generate:entities', methods that generates are wrongly named:
/**
* Add idTipoVendita
*
* #param \AppBundle\Entity\TipiVendita $idTipoVendita
* #return Agenti
*/
public function addIdTipoVenditum(\AppBundle\Entity\TipiVendita $idTipoVendita)
{
$this->idTipoVendita[] = $idTipoVendita;
return $this;
}
/**
* Remove idTipoVendita
*
* #param \AppBundle\Entity\TipiVendita $idTipoVendita
*/
public function removeIdTipoVenditum(\AppBundle\Entity\TipiVendita $idTipoVendita)
{
$this->idTipoVendita->removeElement($idTipoVendita);
}
/**
* Get idTipoVendita
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getIdTipoVendita()
{
return $this->idTipoVendita;
}
How can i have addIdTipoVentita instead of addIdTipoVenditum, removeIdTipoVendita instead of removeIdTipoVenditum?
Update
The issue is that doctrine don't generate properly my entity! It generates methods names with the doctrine inflector component and add these kind of suffixes. How can i configure it in the right way?
Riccardo, You should name your properties in English.
And you should be aware that you are in a Object world, you work with objects and not with object_ids.
So the Entity TipiVendita should be named something like SellType (english, singular).
While the `idTipoVendita' field, if I understand, should enforce an N-M relation with SellType and Agents, so it should be named something like sellTypes (english, plural because it is a collection).
To make a long story short idTipoVendita - beeing a field for a collection, is seen as a plural form and is singularized in idTipoVenditum (just like "Curricula" and "Curriculum").
Best regards.

ApiGility - Returning custom collections

My goal is to return a custom collection for a findAll() query and to deliver this to HAL in order to ensure that its _links are formatted correctly. I originally thought I would simply do this programmatically however this seems to be the wrong way of doing this.
The problem I face is that the data I require is not from a single table, but rather from multiple tables (joins) and I am unable to work out how to do this properly.
I have the following entities:
Stone entity: A standard table with a join to some attributes that I would like to return in my feed
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="Stone\Entity\StAttribute")
* #ORM\JoinTable(name="st_stone_attribute",
* joinColumns={#ORM\JoinColumn(name="stone_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="attribute_id", referencedColumnName="id")}
* )
*
* #var Collection
* #access private
*/
private $attribute;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50, nullable=false)
*/
private $name;
etc...
The attribute entity is a standard table:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=30, nullable=false)
*/
private $name;
My resource calls:
public function fetchAll($params = array())
{
return $this->stoneMapper->fetchAll();
}
My mapper file:
public function fetchAll()
{
$qb = $this->stoneRepository->createQueryBuilder('u')
->where('u.state=:state')
->setParameter('state' , 1 );
$adapter = new DoctrineAdapter( new ORMPaginator( $qb ) );
$collection = new StoneCollection($adapter);
return $collection;
}
My collection
use Zend\Paginator\Paginator;
class StoneCollection extends Paginator
{
}
Screen shot of the results here: http://screencast.com/t/vgm34s92dsk2
As you can see from the screen shot "attribute" and other similar fields are not being populated...
So my question is this: how do I ensure that the join tables are populated in the feed?
You will need to fetch join your associations. You can read on this in the Doctrine 2 documentation here.
In your case it would look as follows:
$qb = $this->stoneRepository->createQueryBuilder('s')
->addSelect('a')
->leftJoin('s.attribute', 'a')
->where('s.state = :state')
->setParameter('state' , 1 );
It will also be necessary to have either a hydrator for your StAttribute in your MetadataMap or there should otherwise be some code implemented to extract the StAttribute properties.
You can of course also do this in the fetch method itself, but that is not so pretty.
The object will continue to render as {} in case you do not extract or convert the object to something that can be serialized to valid json format (either a Hal resource or collection instance, a (json) string or a JsonSerializable).

ManytoMany Relationship in Doctrine 2

I have recently startet with Zend Framework 2 and came now across Doctrine 2, which I would now like to integrate in my first project.
I have now got the following situation and even after days, I can not find a solution.
I have 3 Tables:
Advert
advert_id
advert_title
etc
Category
category_id
name
label
etc
advert2category
advert2category_category_id
advert2category_advert_id
An Advert can be in different Categories and different Categories have different Adverts, therefore the table Advert2Category (ManytoMany).
After reading through the www, I have decided that it should be a ManytoMany Bidirectional, with the "owning side" at the Advert Entity.
Don't ask me why I decided that, I still don't understand Doctrine fully. Anyway, I created 3 Entities, but guess I only need Advert and Category Entity.
I now want the following to happen.
I click on a Category and want to see a list of Articles within this category., that means I have to read out the Table advert2category. I have created the Entities, here my Advert Entity:
So here is first my Advert Entity:
namespace Advert\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Advert
*
* #ORM\Table(name="advert")
* #ORM\Entity
*/
class Advert
{
/**
* #var integer
*
* #ORM\Column(name="advert_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $advertId;
/**
* #var string
*
* #ORM\Column(name="advert_title", type="string", length=255, nullable=true)
*/
private $advertTitle;
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="advertCategory", cascade={"persist"})
* #ORM\JoinTable(name="advert2category",
* joinColumns={#ORM\JoinColumn(name="advert2category_category_id", referencedColumnName="category_id")},
* inverseJoinColumns={#ORM\JoinColumn(name="advert2category_advert_id", referencedColumnName="advert_id")}
* )
*/
protected $category;
public function __construct()
{
$this->category = new ArrayCollection();
}
/**
* Get advertId
*
* #return integer
*/
public function getAdvertId()
{
return $this->advertId;
}
/**
* Set advertTitle
*
* #param string $advertTitle
* #return Advert
*/
public function setAdvertTitle($advertTitle)
{
$this->advertTitle = $advertTitle;
return $this;
}
/**
* Get advertTitle
*
* #return string
*/
public function getAdvertTitle()
{
return $this->advertTitle;
}
/**
* Set category
*
* #param \Advert\Entity\User $category
* #return Advert
*/
public function setCategory(\Advert\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Advert\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
And my Category Entity:
namespace Advert\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity
*/
class Category
{
/**
* #var integer
*
* #ORM\Column(name="category_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\ManyToMany(targetEntity="Advert", mappedBy="category")
**/
private $advertCategory;
public function __construct()
{
$this->advertCategory = new ArrayCollection();
}
/**
* Get categoryId
*
* #return integer
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Just as a first test, I have now tried the following in my Controller:
//Below Controller now works to echo the categories ArrayCollection
$data = $this->getEntityManager()->getRepository('Advert\Entity\Advert')->findAll();
foreach($data as $key=>$row)
{
echo $row->getAdvertTitle();
echo $row->getUser()->getUsername();
$categories = $row->getCategory();
foreach($categories as $row2) {
echo $row2->getName();
}
What am I doing wrong here? Can anyone give me an advice? Thank you very much in advance !
Honestly, and it's a very honest and fine thing, that this is way overcomplicating what you want to do, but only in specific areas.
If you used Composer to include Doctrine (the recommended way), also include symfony/console and you will get a whole mess of awesome tools to help you on your quest. There is a very specific command that will kick you in your seat for how awesome it is: $ doctrine orm:schema-tool:update --force --dump-sql. This will get Doctrine to run through your Entities (you only need the two) and will generate your tables and even setup the *To* associations for you. Int he case of ManyToOne's it will generate the appropriate Foreign Key schema. In the case of ManyToMany's it will automatically create, AND manage it's own association table, you just need only worry about giving the table a name in the Entity.
I'm not kidding you, Do this. It will make your life worth living.
As for your entity setup, this is all you need:
<?php
namespace Advert\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Advert
*
* #ORM\Table(name="advert")
* #ORM\Entity
*/
class Advert
{
/**
* #var integer
*
* #ORM\Column(name="advert_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $advertId;
/**
* #var string
*
* #ORM\Column(name="advert_title", type="string", length=255, nullable=true)
*/
private $advertTitle;
/**
* #ORM\ManyToMany(targetEntity="Category", cascade={"persist"})
* #JoinTable(name="advert_categories")
*/
protected $category;
public function __construct()
{
$this->category = new ArrayCollection();
}
/**
* Get advertId
*
* #return integer
*/
public function getAdvertId()
{
return $this->advertId;
}
/**
* Set advertTitle
*
* #param string $advertTitle
* #return Advert
*/
public function setAdvertTitle($advertTitle)
{
$this->advertTitle = $advertTitle;
return $this;
}
/**
* Get advertTitle
*
* #return string
*/
public function getAdvertTitle()
{
return $this->advertTitle;
}
/**
* Set category
*
* #param ArrayCollection $category
* #return Advert
*/
public function setCategory(ArrayCollection $category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return ArrayCollection
*/
public function getCategory()
{
return $this->category;
}
}
Notice that the getters and setters are Documented to Set and Return ArrayCollection, this is important for IDE's and tools that read PHPDoc and Annotations to understand how in-depth PHP class mapping works.
In addition, notice how much simpler the ManyToMany declaration is? The #JoinTable annotation is there to give a name to the table that doctrine will generate and manage. That's all you need!
But now, you probably should remove the $advertCategory property out of the Category Entity. Doctrine is going to auto-hydrate embedded Entities in properties with the Entity Association Mappings.
This is also potentially dangerous as it can result in infinite recursion. Basically, if all you requested was an Advert with ID of 1, it would go in and find ALL of the Category Entities associated to Advert 1, but inside of those Categories it's re-referencing Advert 1, which Doctrine will sub-query for and inject, which will contain a Category association, which will then Grab those categories, and so on and so fourth until PHP kills itself from lack of memory.
Once everything is good to go, and you got some Categories associated with your Advert, using the Getter for your category in the Advert entity will return an array of Category Entities. Simply iterate through them:
foreach($category as $advert->getCategories()) {
echo $category->getName();
}
or
echo current($advert->getCategories())->getName();

Cascade persisting issues with Doctrine2 and Symfony2

I am using Symfony2 for my application and I am using two Entity Managers; one is for read and other is for write.
I am creating entity manager object like that:
$em = $this->getDoctrine()->getEntityManager('write');
$em = $this->getDoctrine()->getEntityManager('read');
Initially it was working fine but now the following error is coming:
A new entity was found through the relationship 'AppBundle\Entity\ProfileViewer#viewer' that was not configured to cascade persist operations for entity: shamsi. Explicitly persist the new entity or configure cascading persist operations on the relationship.
Here is my ProfileViewer Entity:
/**
* AppBundle\Entity\ProfileViewer
*
* #ORM\Table(name="profile_viewer")
* #ORM\Entity
*/
class ProfileViewer
{
/**
* #var bigint $id
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var bigint $userId
*
* #ORM\Column(name="user_id", type="bigint", nullable=false)
*/
private $userId;
/**
* #var datetime $viewedAt
*
* #ORM\Column(name="viewed_at", type="datetime", nullable=true)
*/
private $viewedAt;
/**
* #ORM\ManyToOne(targetEntity="user", inversedBy="viewers")
* #ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/
private $viewer;
public function __construct()
{
$this->viewedAt = new \DateTime();
}
/**
* Get id
*
* #return bigint
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param bigint $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}
/**
* Get UserId
*
* #return bigint
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set viewedAt
*
* #param datetime $viewedAt
*/
public function setViewedAt($viewedAt)
{
$this->viewedAt = $viewedAt;
}
/**
* Get viewedAt
*
* #return datetime
*/
public function getViewedAt()
{
return $this->viewedAt;
}
/**
* Set viewer
*
* #param AppBundle\Entity\User $viewer
*/
public function setViewer(AppBundle\Entity\User $viewer)
{
$this->viewer = $viewer;
}
/**
* Get viewer
*
* #return AppBundle\Entity\User
*/
public function getViewer()
{
return $this->viewer;
}
}
This error comes when I have created two entity managers.
By default, no operations are cascaded in Doctrine2.
You can add cascade={"persist"} to your association:
/**
* #ORM\ManyToOne(targetEntity="user", inversedBy="viewers", cascade={"persist"})
* #ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/
You can read this to understand cascade operations on associations in doctrine. This is important to underline:
Cascade operations are performed in memory. That means collections and
related entities are fetched into memory, even if they are still
marked as lazy when the cascade operation is about to be performed.
However this approach allows entity lifecycle events to be performed
for each of these operations.
However, pulling objects graph into memory on cascade can cause
considerable performance overhead, especially when cascading
collections are large. Makes sure to weigh the benefits and downsides
of each cascade operation that you define.

Categories