Bonjour,
Voila je rencontre un petit problème avec mon code.
Dans mon application j'ai des utilisateurs normaux et des techniciens les deux utilisateurs.
Un utilisateur doit avoir un technicien.
A la création d'un nouveau utilisateur:
Dans mon form type 'UserType.php' je recupere les utilisateurs Technicien avec la base de donnée dans un select.
<?php
namespace App\Form;
use App\Entity\Users;
use App\Repository\UsersRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname')
->add('lastname')
->add('email')
->add('phone')
->add('city')
->add('status', ChoiceType::class, [
'choices' => $this->getChoices()
])
->add('certification_phyto')
->add('technician', EntityType::class, array(
'class' => Users::class,
'query_builder' => function(UsersRepository $user) {
return $user->createQueryBuilder('u')
->orderBy('u.firstname', 'ASC')
->andWhere('u.status = :status')
->setParameter('status', 'ROLE_TECHNICIAN');
},
'choice_label' => function(Users $user) {
return $user->getFirstname() . ' ' . $user->getLastname(); },
'choice_value' => 'id'
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Users::class,
'translation_domain' => 'forms'
]);
}
private function getUsers(UsersRepository $user) {
return $user->createQueryBuilder('u')
->orderBy('u.firstname', 'ASC')
->andWhere('u.status = :status')
->setParameter('status', 'ROLE_TECHNICIAN');
}
private function getChoices()
{
$choices = Users::STATUS;
$output = [];
foreach ($choices as $k => $v) {
$output[$v] = $k;
}
return $output;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
* @UniqueEntity("email")
*/
class Users implements UserInterface, \Serializable
{
const STATUS = [
'ROLE_USER' => 'Client',
'ROLE_TECHNICIAN' => 'Technicien',
'ROLE_ADMIN' => 'Administrateur'
];
const ISACTIVE = [
1 => 'Activé',
2 => 'En attente'
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=30)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=30)
*/
private $email;
/**
* @ORM\Column(type="string", length=30)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=30)
*/
private $phone;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $city;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=30)
*/
private $status;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_activity;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $technician;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $isActive;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $certification_phyto;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getTechnician(): ?string
{
return $this->technician;
}
public function setTechnician(string $technician): self
{
$this->technician = $technician;
return $this;
}
public function getLastActivity(): ?\DateTimeInterface
{
return $this->last_activity;
}
public function setLastActivity(?\DateTimeInterface $last_activity): self
{
$this->last_activity = $last_activity;
return $this;
}
public function getCertificationPhyto(): ?string
{
return $this->certification_phyto;
}
public function setCertificationPhyto(?string $certification_phyto): self
{
$this->certification_phyto = $certification_phyto;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return array($this->getStatus());
}
/**
* @inheritDoc
*/
public function getSalt()
{
return null;
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->getEmail();
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @inheritDoc
*/
public function serialize()
{
return serialize([
$this->id,
$this->email,
$this->password
]);
}
/**
* @inheritDoc
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->email,
$this->password
) = unserialize($serialized, ['allowed_classes' => false]);
}
}
Quand je submit mon form il faudrait que ma colonne technician en DB prend l'ID du technicien choisi.
Mais j'arrive sur cette erreur : Expected argument of type "string", "App\Entity\Users" given.
Ma colonne est bien déclaré en string et j'envoie un id ou 2 chiffres donc je comprend pas.
Merci beaucoup de vos réponses ou aide
Cordialement