I have a problem creating an entity Annonce
. In an Annonce
, I have a Categorie
.
The creation of an Annonce
used to work, but now, it does not work anymore (I don't know why).
When I submit the creation form, all the categories are deleted (I see it in the doctrine profiler) and then it tries to save the annonce but the category I have selected in the form does not exists anymore as it has been deleted just before.
I don't know why there is those deletions. Here is my code :
AnnonceController newAction :
/**
* Creates a new annonce entity.
*
* @Route("/nouvelle", name="ad_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$annonce = new Annonce();
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$annonce->setUser($this->getUser());
}
$em = $this->getDoctrine()->getManager();
$repoCategorie = $em->getRepository('AnnoncesBundle:Categorie');
$rootCategories = $repoCategorie->findRootCategories();
foreach ($rootCategories as $rootCategory)
{
$rootCategory->setEnfants($repoCategorie->findByParent($rootCategory));
}
$form = $this->createForm('AnnoncesBundle\Form\AnnonceType', $annonce);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$annonce->setComplexId(uniqid('', true));
$annonce->setDateCreation(new \DateTime("now"));
$annonce->setDateModification(new \DateTime("now"));
$em->persist($annonce);
$em->flush();
// On traite les image
$this->gerePhotos($annonce);
$stat = $em->getRepository("AppBundle:Stats")->find(1);
$stat->setNbAnnonces($stat->getNbAnnonces() + 1);
$em->flush();
$urlHelper = $this->container->get('AppBundle\Services\UrlHelper');
return $this->redirectToRoute('ad_confirme', array('id' => $annonce->getId(), 'complexId' => $annonce->getComplexId()));
}
return $this->render('annonce/new.html.twig', array(
'annonce' => $annonce,
'form' => $form->createView(),
'categories' => $rootCategories
));
}
Categorie
is defined in the Annonce
entity like that :
/**
* @ORM\ManyToOne(targetEntity="AnnoncesBundle\Entity\Categorie")
* @ORM\JoinColumn(name="categorie_code", referencedColumnName="code")
* @Assert\NotBlank(message="Merci de renseigner la catégorie")
*/
private $categorie;
The Categorie
entity code:
<?php
namespace AnnoncesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categorie
*
* @ORM\Table(name="categorie")
* @ORM\Entity(repositoryClass="AnnoncesBundle\Repository\CategorieRepository")
*/
class Categorie
{
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var int
*
* @ORM\Column(name="position_affichage", type="integer")
*/
private $positionAffichage;
/**
* @var string
* @ORM\Id
* @ORM\Column(name="code", type="string", length=255)
*/
private $code;
/**
* Many Categories have One Category.
* @ORM\ManyToOne(targetEntity="AnnoncesBundle\Entity\Categorie", inversedBy="enfants")
* @ORM\JoinColumn(name="parent_code", referencedColumnName="code", nullable=true)
*/
private $parent;
/**
* One Category has Many children Categories.
* @ORM\OneToMany(targetEntity="AnnoncesBundle\Entity\Categorie", mappedBy="parent",orphanRemoval=true, cascade={"remove"})
*/
private $enfants;
public function __construct() {
$this->enfants = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @param string $code
*/
public function setCode($code)
{
$this->code = $code;
}
/**
* @return int
*/
public function getPositionAffichage()
{
return $this->positionAffichage;
}
/**
* @param int $positionAffichage
*/
public function setPositionAffichage($positionAffichage)
{
$this->positionAffichage = $positionAffichage;
}
/**
* Set nom
*
* @param string $nom
*
* @return Categorie
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Get nom complet
*
* @return string
*/
public function getNomComplet()
{
if ($this->parent)
{
return $this->parent->getNom() . " > " . $this-> nom;
}
return $this->nom;
}
/**
* Set parent
*
* @param Categorie $parent
*
* @return Categorie
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return Categorie
*/
public function getParent()
{
return $this->parent;
}
/**
* @return mixed
*/
public function getEnfants()
{
return $this->enfants;
}
/**
* @param mixed $enfants
*/
public function setEnfants($enfants)
{
$this->enfants = $enfants;
}
public function __toString()
{
return "".$this->code;
}
}
And the Doctrine profiler logs :
17 0.28 ms
"START TRANSACTION"
Parameters:
[]
View formatted query View runnable query Explain query
18 11.37 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"enfants"
]
View formatted query View runnable query Explain query
19 115.71 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"services"
]
View formatted query View runnable query Explain query
20 0.54 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"jardin"
]
View formatted query View runnable query Explain query
21 0.61 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"art"
]
View formatted query View runnable query Explain query
22 0.96 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"loisirs"
]
View formatted query View runnable query Explain query
23 0.62 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"maison"
]
View formatted query View runnable query Explain query
24 0.36 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"vehicules"
]
View formatted query View runnable query Explain query
25 0.34 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"immobilier"
]
View formatted query View runnable query Explain query
26 0.33 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"emploi"
]
View formatted query View runnable query Explain query
27 0.28 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"electronique"
]
View formatted query View runnable query Explain query
28 0.27 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"animaux"
]
View formatted query View runnable query Explain query
29 0.19 ms
DELETE FROM categorie WHERE parent_code = ?
Parameters:
[▼
"divers"
]
View formatted query View runnable query Explain query
30 100.67 ms
INSERT INTO annonce (complex_id, titre, prix, monnaie, monnaie_paiement, type, texte, troc, photos, show_tel, date_creation, date_modif, date_renvoi_mail_confirmation_creation, confirme, validee, refusee, paiement_effectue, pack_urgent, pack_photo, pack_remontee, frequence_remontee, jour_remontee, nb_remontees_restantes, date_alaune, date_derniere_mise_alaune, date_fin_alaune, taille, taille_enfant, marque, modele, date_mise_circulation, energie_vehicule, boite_vitesse, kilometrage, type_bien, surface, nb_pieces, classe_energie, ges, meuble, capacite_personnes, type_contrat, remuneration, experience, temps_plein, handi_acces, duree, categorie_code, user_id, anonymousUser_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Parameters:
[▼
1 => "5b4864d0efb892.92513412"
2 => "tesq"
3 => null
4 => "euro"
5 => "euro"
6 => "offre"
7 => "desc"
8 => 0
9 => null
10 => 0
11 => "2018-07-13 10:37:36"
12 => "2018-07-13 10:37:36"
13 => null
14 => 0
15 => 0
16 => 0
17 => 0
18 => 0
19 => 0
20 => 0
21 => null
22 => null
23 => null
24 => null
25 => null
26 => null
27 => null
28 => null
29 => null
30 => null
31 => null
32 => null
33 => null
34 => null
35 => null
36 => null
37 => null
38 => null
39 => null
40 => null
41 => null
42 => null
43 => null
44 => null
45 => 0
46 => 0
47 => null
48 => "enfants_livres"
49 => 11
50 => null
]
View formatted query View runnable query Explain query
31 43.06 ms
"ROLLBACK"
Parameters:
[]
View formatted query View runnable query Explain query
Anyone has a clue to give me to understand why these Categorie
entities are deleted ?
Thanks !