Salut la grande famille des développeurs back :)
Voila, je rencontre un petit voir un sacré problème depuis plusieurs jours à vrai dire.
Le topo est que j'ai deux entités, Company et Group. ( L'entité maître est Company et donc esclave est Group )
J'aimerai ajouté un group à une company mais il m'est impossible d'y arriver....
Je tombe sur cette erreur lorsque je valide le formulaire d'ajout :
An exception occurred while executing 'INSERT INTO group (title, company_id) VALUES (?, ?)' with params ["test", 5]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group (title, company_id) VALUES ('test', 5)' at line 1
Voici l'entité Company :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
* @UniqueEntity(fields="title", message="Cette entreprise existe déjà !!!")
*/
class Company
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $contactCompany;
/**
* @ORM\Column(type="string", length=255)
*/
private $contactName;
/**
* @ORM\Column(type="string", length=255)
*/
private $contactEmail;
/**
* @ORM\Column(type="string", length=255)
*/
private $contactPhone;
/**
* @ORM\Column(type="string", length=255)
*/
private $address;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $address2;
/**
* @ORM\Column(type="string", length=255)
*/
private $zip;
/**
* @ORM\Column(type="string", length=255)
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
*/
private $country;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $website;
/**
* @ORM\Column(type="text")
*/
private $permissionReminder;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Group", mappedBy="company", cascade={"remove"})
*/
private $groups;
public function __construct()
{
$this->groups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContactCompany(): ?string
{
return $this->contactCompany;
}
public function setContactCompany(string $contactCompany): self
{
$this->contactCompany = $contactCompany;
return $this;
}
public function getContactName(): ?string
{
return $this->contactName;
}
public function setContactName(string $contactName): self
{
$this->contactName = $contactName;
return $this;
}
public function getContactEmail(): ?string
{
return $this->contactEmail;
}
public function setContactEmail(string $contactEmail): self
{
$this->contactEmail = $contactEmail;
return $this;
}
public function getContactPhone(): ?string
{
return $this->contactPhone;
}
public function setContactPhone(string $contactPhone): self
{
$this->contactPhone = $contactPhone;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getAddress2(): ?string
{
return $this->address2;
}
public function setAddress2(?string $address2): self
{
$this->address2 = $address2;
return $this;
}
public function getZip(): ?string
{
return $this->zip;
}
public function setZip(string $zip): self
{
$this->zip = $zip;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
public function getPermissionReminder(): ?string
{
return $this->permissionReminder;
}
public function setPermissionReminder(string $permissionReminder): self
{
$this->permissionReminder = $permissionReminder;
return $this;
}
/**
* @return Collection|Group[]
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
$group->setCompany($this);
}
return $this;
}
public function removeGroup(Group $group): self
{
if ($this->groups->contains($group)) {
$this->groups->removeElement($group);
// set the owning side to null (unless already changed)
if ($group->getCompany() === $this) {
$group->setCompany(null);
}
}
return $this;
}
}
Voici l'entité Group :
<?php
namespace App\Entity;
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="App\Repository\GroupRepository")
*/
class Group
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="groups")
* @ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="groups", cascade={"remove"})
*/
private $user;
public function __construct()
{
$this->user = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getCompany(): ?company
{
return $this->company;
}
public function setCompany(?company $company): self
{
$this->company = $company;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(User $user): self
{
if (!$this->user->contains($user)) {
$this->user[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->user->contains($user)) {
$this->user->removeElement($user);
}
return $this;
}
}
Et voici le formulaire d'ajout d'un group à une company :
<?php
namespace App\Form;
use App\Entity\User;
use App\Entity\Group;
use App\Repository\UserRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class FormAddGroupType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'Nom du groupe'
])
->add('user', EntityType::class, [
'class' => User::class,
'label' => 'Liste des utlisateurs',
'query_builder' => function (UserRepository $ur)
{
return $ur->createQueryBuilder('u')
->orderBy('u.firstname', 'ASC');
},
'choice_label' => 'firstname',
'expanded' => true,
'multiple' => true
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Group::class,
]);
}
}
Je ne l'ai peut être pas précisé, mais quand j'ajoute un group à une company, j'envoie l'id de la company dans l'url histoire de l'injecter directement dans le formulaire enfin dans l'entité.
Merci de votre précieuse aide :D
Bonjour,
"group" étant un mot réserver à SQL, je pense que l'origine de ton problème viens de là
Entoure le de ` (alt gr + 7)