symfony2 persist manyToOne - symfony

i Have a single page application and i use symfony as a Rest API.
In my object "ResidenceIntervenant, i have a ManyToOne relation :
manyToOne:
intervenant:
targetEntity: Intervenant
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
intervenant_id:
referencedColumnName: id
orphanRemoval: false
When i do this :
$myData = json_decode($request->getContent(), true);
$intervenant = $this->em->getRepository('AppBundle:Intervenant')->find($intervenantId);
$relation = new ResidenceIntervenant();
$myData['intervenant'] = $intervenant->getId();
$form_relation = $this->formFactory->create(ResidenceIntervenantType::class, $relation, ['method' => "POST"]);
$form_relation->submit($myData, TRUE);
if ( ! $form_relation->isValid()) {
$this->em->persist($relation);
$this->em->flush();
}
it works and i have the id in my table
When i do :
$myData = json_decode($request->getContent(), true);
$intervenant = $this->em->getRepository('AppBundle:Intervenant')->find($intervenantId);
$relation = new ResidenceIntervenant();
$relation->setIntervenant($intervenant);
$form_relation = $this->formFactory->create(ResidenceIntervenantType::class, $relation, ['method' => "POST"]);
$form_relation->submit($myData, TRUE);
if ( ! $form_relation->isValid()) {
$this->em->persist($relation);
$this->em->flush();
}
it doesn't persists the id
Is this normal ?
my FormType biuldForm method :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('contratNum')
->add('appareilNum')
->add('intervenantOrigine')
->add('intervenant')
->add('residence');
}
Thanks for your help
EDIT : add informations to show my entities
I tried to add these linee but it neither works:
$intervenant->addResidenceIntervenant($relation);
$this->em->persist($intervenant);
$this->em->flush();
ResidenceIntervenant Entity :
<?php
namespace AppBundle\Entity;
/**
* ResidenceIntervenant
*/
class ResidenceIntervenant
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $contratNum;
/**
* #var string
*/
private $appareilNum;
/**
* #var boolean
*/
private $intervenantOrigine;
/**
* #var \AppBundle\Entity\Intervenant
*/
private $intervenant;
/**
* #var \AppBundle\Entity\Residence
*/
private $residence;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set contratNum
*
* #param string $contratNum
*
* #return ResidenceIntervenant
*/
public function setContratNum($contratNum)
{
$this->contratNum = $contratNum;
return $this;
}
/**
* Get contratNum
*
* #return string
*/
public function getContratNum()
{
return $this->contratNum;
}
/**
* Set appareilNum
*
* #param string $appareilNum
*
* #return ResidenceIntervenant
*/
public function setAppareilNum($appareilNum)
{
$this->appareilNum = $appareilNum;
return $this;
}
/**
* Get appareilNum
*
* #return string
*/
public function getAppareilNum()
{
return $this->appareilNum;
}
/**
* Set intervenantOrigine
*
* #param boolean $intervenantOrigine
*
* #return ResidenceIntervenant
*/
public function setIntervenantOrigine($intervenantOrigine)
{
$this->intervenantOrigine = $intervenantOrigine;
return $this;
}
/**
* Get intervenantOrigine
*
* #return boolean
*/
public function getIntervenantOrigine()
{
return $this->intervenantOrigine;
}
/**
* Set intervenant
*
* #param \AppBundle\Entity\Intervenant $intervenant
*
* #return ResidenceIntervenant
*/
public function setIntervenant(\AppBundle\Entity\Intervenant $intervenant = null)
{
$this->intervenant = $intervenant;
return $this;
}
/**
* Get intervenant
*
* #return \AppBundle\Entity\Intervenant
*/
public function getIntervenant()
{
return $this->intervenant;
}
/**
* Set residence
*
* #param \AppBundle\Entity\Residence $residence
*
* #return ResidenceIntervenant
*/
public function setResidence(\AppBundle\Entity\Residence $residence = null)
{
$this->residence = $residence;
return $this;
}
/**
* Get residence
*
* #return \AppBundle\Entity\Residence
*/
public function getResidence()
{
return $this->residence;
}
}
ResidenceIntervenant.orm.yml
AppBundle\Entity\ResidenceIntervenant:
type: entity
table: residence_intervenant
indexes:
fk_residence_intervenant_interv_id_idx:
columns:
- intervenant_id
fk_residence_intervenant_res_id_idx:
columns:
- residence_id
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
contratNum:
type: string
nullable: true
length: 100
options:
fixed: false
column: contrat_num
appareilNum:
type: string
nullable: true
length: 100
options:
fixed: false
column: appareil_num
intervenantOrigine:
type: boolean
nullable: false
options:
default: false
column: intervenant_origine
manyToOne:
intervenant:
targetEntity: Intervenant
cascade: ["persist"]
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
intervenant_id:
referencedColumnName: id
orphanRemoval: false
residence:
targetEntity: Residence
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
residence_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
Intervenant Entity :
<?php
namespace AppBundle\Entity;
/**
* Intervenant
*/
class Intervenant
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $libelleContact;
/**
* #var string
*/
private $url;
/**
* #var \AppBundle\Entity\Metier
*/
private $metier;
/**
* #var \AppBundle\Entity\Tiers
*/
private $tiers;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelleContact
*
* #param string $libelleContact
*
* #return Intervenant
*/
public function setLibelleContact($libelleContact)
{
$this->libelleContact = $libelleContact;
return $this;
}
/**
* Get libelleContact
*
* #return string
*/
public function getLibelleContact()
{
return $this->libelleContact;
}
/**
* Set url
*
* #param string $url
*
* #return Intervenant
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set metier
*
* #param \AppBundle\Entity\Metier $metier
*
* #return Intervenant
*/
public function setMetier(\AppBundle\Entity\Metier $metier = null)
{
$this->metier = $metier;
return $this;
}
/**
* Get metier
*
* #return \AppBundle\Entity\Metier
*/
public function getMetier()
{
return $this->metier;
}
/**
* Set tiers
*
* #param \AppBundle\Entity\Tiers $tiers
*
* #return Intervenant
*/
public function setTiers(\AppBundle\Entity\Tiers $tiers = null)
{
$this->tiers = $tiers;
return $this;
}
/**
* Get tiers
*
* #return \AppBundle\Entity\Tiers
*/
public function getTiers()
{
return $this->tiers;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $residenceIntervenant;
/**
* Constructor
*/
public function __construct()
{
$this->residenceIntervenant = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add residenceIntervenant
*
* #param \AppBundle\Entity\ResidenceIntervenant $residenceIntervenant
*
* #return Intervenant
*/
public function addResidenceIntervenant(\AppBundle\Entity\ResidenceIntervenant $residenceIntervenant)
{
$this->residenceIntervenant[] = $residenceIntervenant;
return $this;
}
/**
* Remove residenceIntervenant
*
* #param \AppBundle\Entity\ResidenceIntervenant $residenceIntervenant
*/
public function removeResidenceIntervenant(\AppBundle\Entity\ResidenceIntervenant $residenceIntervenant)
{
$this->residenceIntervenant->removeElement($residenceIntervenant);
}
/**
* Get residenceIntervenant
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getResidenceIntervenant()
{
return $this->residenceIntervenant;
}
}
Intervenant.orm.yml :
AppBundle\Entity\Intervenant:
type: entity
table: intervenant
indexes:
fk_intervenant_metier_id_idx:
columns:
- metier_id
fk_intervenant_tiers_id_idx:
columns:
- tiers_id
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
libelleContact:
type: string
nullable: false
length: 255
options:
fixed: false
column: libelle_contact
url:
type: string
nullable: true
length: 255
options:
fixed: false
oneToMany:
residenceIntervenant:
targetEntity: ResidenceIntervenant
mappedBy: intervenant
cascade: [remove]
oneToOne:
tiers:
targetEntity: Tiers
joinColumn:
name: tiers_id
referencedColumnName: id
cascade: [remove, persist]
manyToOne:
metier:
targetEntity: Metier
cascade: ["persist"]
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
metier_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
EDIT : Problem solved
i updated addResidenceIntervenant like this :
public function addResidenceIntervenant(\AppBundle\Entity\ResidenceIntervenant $residenceIntervenant)
{
$this->residenceIntervenant[] = $residenceIntervenant;
$residenceIntervenant->setIntervenant($this);
return $this;
}
i added these lines after persisting my relation :
$intervenant->addResidenceIntervenant($relation);
$this->em->persist($intervenant);

Your second example has this line:
$relation->setIntervenant($intervenant);
Does the method setIntervenant() set the relation?
I think you should do something like this:
public function setIntervenant(Intervenant $intervenant)
{
$this->intervenant = $intervenant;
$intervenant->setResidence($this);
}
Anyway, the relations in Doctrine can be unidirectional or bidirectional.
Your Many-To-One relation seems to be unidirectional. You should set a One-To-Many relation on your Intervenant entity.
Read more about this in the Doctrine documentation:
Many-to-One, Unidirectional (This is what you did)
One-to-Many, bidirectional (This is what you should do)
One-To-Many, Unidirectional with Join Table (Just for completeness)
So, you should trait your Intervenant as the Doctrine treats the Feature while your ResidenceIntervenant has to be treated as the Product in the Doctrine documentation.

Related

Symfony sonata - form mapper

in symfony sonata :
I have an object CONTACT contains many ROLE -
i want to see all was inside an entity role in form mapper:
my entity role have 4 parameters.
(label function, phone, email, etc...)
actually i just have a link to the object.
(but i want to see all the parameters was inside the entity)
i try this in my form mapper of my class ADMIN
$showMapper
->with('CONTACT - FUNCTION')
->add('role')
->end()
namespace AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Role
*/
class Role
{
/**
* #var int
*/
private $id;
/**
* #var string(unique=true)
*/
private $function;
/**
* #var int
*/
private $organisation;
/**
* #var string
*/
private $phone;
/**
* #var string
*/
private $email;
/**
* #var int
*/
private $contact=null;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
public function __toString(){
return sprintf("%s %s", $this->getFunction(), $this->getOrganisation());
}
public function getFunction_name()
{
return $this->getFunction();
}
/**
* Set contact
*
* #param int $contact
*
* #return role
*/
public function setContact($contact)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* #return int
*/
public function getContact()
{
return $this->contact;
}
/**
* Set function
*
* #param string $function
*
* #return Role
*/
public function setFunction($function)
{
$this->function = $function;
return $this;
}
/**
* Get function
*
* #return string
*/
public function getFunction()
{
return $this->function;
}
/**
* Set organisation
*
* #param int $organisation
*
* #return Role
*/
public function setOrganisation($organisation)
{
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* #return int
*/
public function getOrganisation()
{
return $this->organisation;
}
/**
* Set phone
*
* #param string $phone
*
* #return Role
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set email
*
* #param string $email
*
* #return Role
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
}
and my doctrine file
AdminBundle\Entity\Role:
type: entity
table: null
repositoryClass: AdminBundle\Repository\RoleRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
function:
type: string
length: 100
unique: true
phone:
type: string
length: 100
nullable: TRUE
email:
type: string
length: 100
nullable: TRUE
manyToOne:
organisation:
targetEntity: AdminBundle\Entity\Organisation
joinColumn:
name: organisation
referencedColumnName: id
nullable: TRUE
manyToMany:
contact:
targetEntity: AdminBundle\Entity\Contact
joinTable:
name: allrole
joinColumns:
role:
referencedColumnName: id
inverseJoinColumns:
contact:
referencedColumnName: id
Create a __toString() method into your Role entity with properties you want to display.
public function __toString()
{
return (string) "Function: " . $this->function . ", Phone: " . $this->phone . ", Email: " . $this->email;
}

manyToMany not working in listener with virtual property

I have a manyToMany relation beetween "Lot" and "BailProprietaire"
When i get an entity "BailProprietaire", i see the entities "lot" linked
But when i get an entity "Lot", i don't see entities "BailProprietaire" linked
In lot.orm.yml, i have :
AppBundle\Entity\Lot:
type: entity
repositoryClass: AppBundle\Repository\LotRepository
table: lot
....
....
manyToMany:
bauxProprietaire:
targetEntity: BailProprietaire
mappedBy: lots
In bailProprietaire.orm.yml, i have :
AppBundle\Entity\BailProprietaire:
type: entity
table: bail_proprietaire
repositoryClass: AppBundle\Repository\BailProprietaireRepository
....
....
manyToMany:
lots:
targetEntity: Lot
inversedBy: bauxProprietaire
fetch: LAZY
joinTable:
name: bail_proprietaire_lots
joinColumns:
bail_id:
referencedColumnName: id
inverseJoinColumns:
lot_id:
referencedColumnName: id
lifecycleCallbacks: { }
do you see something i miss ?
thanks
EDIT : Add php entity code
Lot.php
class Lot
{
/**
* #var integer
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $bauxProprietaire;
/**
* Constructor
*/
public function __construct()
{
$this->bauxProprietaire = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add bauxProprietaire
*
* #param \AppBundle\Entity\BailProprietaire $bauxProprietaire
*
* #return Lot
*/
public function addBauxProprietaire(\AppBundle\Entity\BailProprietaire $bauxProprietaire)
{
$this->bauxProprietaire[] = $bauxProprietaire;
return $this;
}
/**
* Remove bauxProprietaire
*
* #param \AppBundle\Entity\BailProprietaire $bauxProprietaire
*/
public function removeBauxProprietaire(\AppBundle\Entity\BailProprietaire $bauxProprietaire)
{
$this->bauxProprietaire->removeElement($bauxProprietaire);
}
/**
* Get bauxProprietaire
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBauxProprietaire()
{
return $this->bauxProprietaire;
}
}
BailProprietaire.php
class BailProprietaire
{
/**
* #var integer
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $lots;
/**
* Constructor
*/
public function __construct()
{
$this->lots = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add lot
*
* #param \AppBundle\Entity\Lot $lot
*
* #return BailProprietaire
*/
public function addLot(\AppBundle\Entity\Lot $lot)
{
$this->lots[] = $lot;
return $this;
}
/**
* Remove lot
*
* #param \AppBundle\Entity\Lot $lot
*/
public function removeLot(\AppBundle\Entity\Lot $lot)
{
$this->lots->removeElement($lot);
}
/**
* Get lots
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getLots()
{
return $this->lots;
}
}
EDIT 2 : in fact, it works but not with the listener
in fact i see the entities "BailProprietaire" when i get a "lot" but when i flush data, i have a listener. In this listener, i call a virtual propertie of "Lot.php" where i have :
if (!empty($this->bauxProprietaire)) {
....
} else {
....
}
but $this->bauxProprietaire is always empty
Ok, i found the problem.
When i do $this->bauxProprietaire, i have a "Doctrine\ORM\PersistentCollection" but when i look the collection in this object, there is 0 element
but if i do $this->bauxProprietaire->toArray(), i see my relation
I don't understand why but it works

Many to One Relation in Symfony 2

I have a problem related to Doctrine2:
1- I have two tables joining on a many-to-one relation:
Table 1 - Activity
The Schema:
Backend\adminBundle\Entity\Activity:
type: entity
table: activity
indexes:
result_id:
columns:
- result_id
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
......
manyToOne:
result:
targetEntity: Actionresult
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
result_id:
referencedColumnName: id
orphanRemoval: false
The Entity
<?php
namespace Backend\adminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Activity {
/**
* #var \Backend\adminBundle\Entity\Actionresult
*
* #ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\Actionresult")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="result_id", referencedColumnName="id")
* })
*/
private $result;
/**
* #var \Backend\adminBundle\Entity\SfGuardUser
*
* #ORM\ManyToOne(targetEntity="Backend\adminBundle\Entity\SfGuardUser")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
/* There are other Properties */
/**
* Set result
*
* #param \Backend\adminBundle\Entity\Actionresult $result
* #return Activity
*/
public function setResult(\Backend\adminBundle\Entity\Actionresult $result = null)
{
$this->result = $result;
return $this;
}
/**
* Get result
*
* #return \Backend\adminBundle\Entity\Actionresult
*/
public function getResult()
{
return $this->result;
}
}
Table 2 - Actionresult Related to Activity Table by Id:
The schema:
Backend\adminBundle\Entity\Actionresult:
type: entity
table: actionresult
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
name:
type: string
nullable: false
length: 255
fixed: false
comment: ''
lifecycleCallbacks: { }
The Entity:
<?php
namespace Backend\adminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Actionresult
*
* #ORM\Table(name="actionresult")
* #ORM\Entity
*/
class Actionresult
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Actionresult
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
The Question:
With doctrine i can refer from table Activity to Actionresult with the name result.
How can i refer with doctrine from table Actionresult to Activity??
Thank you in advance.
To be thorough, you should try and stick to one type of entity mapping in Symfony whenever possible. The #ORM* annotations are redundant if you use YAML config, and vice-versa. I'll provide the answer using YAML, and I believe you'll be able to convert to annotations if need be.
# Activity.yml
Activity:
type: entity
...
manyToOne:
result:
targetEntity: ActionResult
inversedBy: activities
# ActionResult.yml
Result:
type: entity
oneToMany:
activities:
targetEntity: Activity
mappedBy: result
# ActionResult.php
class Result {
protected $activities;
public function __construct()
{
$this->activities = new Doctrine\Common\Collections\ArrayCollection();
}
public function getActivities()
{
return $this->activities;
}
public function addActivity(Activity $activity)
{
$activity->setResult($this);
$this->activities->add($activity);
}
public function removeActivity(Activity $activity)
{
$activity->setResult(null);
$this->activities->removeElement($activity);
}
}
# Activity.php
class Activity {
protected $result;
public function getResult()
{
return $this->result;
}
public function setResult(ActionResult $result = null)
{
$this->result = $result;
}
}
Reference:
Bidirectional one to many: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional

Symfony2: Error "Property was already declared, but it must be declared only once"

As I am new to Symfony, I tried creating the entity relationship using Doctrine. I am getting the error Property "report" in "[bundle/entity/file_location" was already declared, but it must be declared only once" when I try to update the schema.
I have followed the Symfony documentation, but could not find the solution.
Entity/Report.php
<?php
namespace Aurora\ReportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Report
*/
class Report
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $name;
/**
* #var string
*/
private $description;
/**
* var array
*/
private $reportFiles;
public function _construct() {
$this->reportFiles = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Report
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Report
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
}
Entity/ReportFile.php
<?php
namespace Aurora\ReportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ReportFile
*/
class ReportFile
{
/**
* #var integer
*/
private $id;
/**
* #var Report
*/
private $report;
/**
* #var string
*/
private $name;
/**
* #var string
*/
private $path;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get report
*
* #return integer
*/
public function getReport()
{
return $this->report;
}
/**
* Set report
*
* #param integer $report
* #return ReportFile
*/
public function setReport($report)
{
$this->report = $report;
return $this;
}
/**
* Set name
*
* #param string $name
* #return ReportFile
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path
*
* #param string $path
* #return ReportFile
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
}
Doctrine/Report.orm
Aurora\ReportBundle\Entity\Report:
type: entity
table: null
repositoryClass: Aurora\ReportBundle\Entity\ReportRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
description:
type: text
lifecycleCallbacks: { }
oneToMany:
reportFiles:
targetEntity: ReportFile
mappedBy: report_id
Doctrine/ReportFile.orm.yml
Aurora\ReportBundle\Entity\ReportFile:
type: entity
table: null
repositoryClass: Aurora\ReportBundle\Entity\ReportFileRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
report:
type: integer
column: report_id
name:
type: string
length: 255
path:
type: string
length: 255
lifecycleCallbacks: { }
manyToOne:
report:
targetEntity: Report
inversedBy: reportFiles
joinColumn:
name: report_id
referencedColumnName: id
In Doctrine you shouldn't to declare relation columns as fields.
Remove report field from Doctrine/ReportFile.orm.yml but leave manyToOne relation. Doctrine will create column by itself.

Doctrine entity proxy exception

I have a big problem with one of my entities in my Symfony project.
Some code first:
Address entity
<?php
namespace AppBundle\Entity;
class Address
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $street;
/**
* #var string|null
*/
private $postalCode;
/**
* #var string|null
*/
private $city;
/**
* #var string|null
*/
private $province;
/**
* #var float
*/
private $latitude;
/**
* #var float
*/
private $longtitude;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set street
*
* #param string $street
* #return Address
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* Get street
*
* #return string
*/
public function getStreet()
{
return $this->street;
}
/**
* Set postalCode
*
* #param integer $postalCode
* #return Address
*/
public function setPostalCode($postalCode)
{
$this->postalCode = $postalCode;
return $this;
}
/**
* Get postalCode
*
* #return integer
*/
public function getPostalCode()
{
return $this->postalCode;
}
/**
* Set city
*
* #param string $city
* #return Address
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set province
*
* #param string $province
* #return Address
*/
public function setProvince($province)
{
$this->province = $province;
return $this;
}
/**
* Get province
*
* #return string
*/
public function getProvince()
{
return $this->province;
}
/**
* Set latitude
*
* #param string $latitude
* #return Address
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longtitude
*
* #param string $longtitude
* #return Address
*/
public function setLongtitude($longtitude)
{
$this->longtitude = $longtitude;
return $this;
}
/**
* Get longtitude
*
* #return string
*/
public function getLongtitude()
{
return $this->longtitude;
}
}
Address entity mapping:
AppBundle\Entity\Address:
type: entity
table: addresses
id:
id:
type: integer
generator:
strategy: AUTO
fields:
street:
type: string
nullable: false
postalCode:
name: postal_code
type: string
nullable: true
city:
type: string
nullable: false
province:
type: string
nullable: true
latitude:
type: decimal
scale: 12
precision: 18
nullable: true
longtitude:
type: decimal
scale: 12
precision: 18
nullable: true
Venue entity mapping:
AppBundle\Entity\Venue (shortened for the sake of example):
type: entity
table: venues
manyToOne:
address:
targetEntity: AppBundle\Entity\Address
joinColumn:
name: address_id
referencedColumnName: id
nullable: false
cascade: ["persist"]
The problem is that I face an exception being thrown:
Notice: Array to string conversion 500 Internal Server Error -
ContextErrorException
here $proxyCode = strtr($this->proxyClassTemplate, $venueholders); (in vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php at line 280).
When I remove the relation, everything works correctly so it tells me there's some kind of issue with Address entity.
I've tried to clear the cache - no luck. Mapping looks ok to me, getters/setters are correct.
Any tips?
Did you try to reinstall your vendors completely? I see one issue in your question: $proxyCode = strtr($this->proxyClassTemplate, $venueholders);.
Line 280 of ProxyGenerator class should look like this: https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Proxy/ProxyGenerator.php#L280.
Did you try to apply search & replace on your code by any chance?
Try column: instead of name:
postalCode:
column: postal_code
type: string
nullable: true

Resources