我制作了一個表格來創建事件。首先,用戶必須創建組態檔并進行連接。他有自己的地址串列。目前,他可以在表單中選擇任何地址,甚至是其他用戶創建的地址。我想讓他只看到他在放置選擇中創建的地址。
在EventType檔案中,我使用
EntityType::classLocation 物體,知道多虧了它創建了地址。在位置的物體檔案中,創建“組織者”列是為了知道誰創建了地址。它與 ManyToOne to User 類相關聯。
我在哪里必須指定用戶必須在表單中只選擇他自己的地址?
事件型別.php
<?php
namespace App\Form;
use App\Entity\Event;
use App\Entity\Language;
use App\Entity\Location;
use App\Entity\Category;
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\TimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class EventType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
->add('description', TextareaType::class)
->add('spokenlanguage', EntityType::class, [
'class' => Language::class,
'choice_label' => 'name',
'placeholder' => 'Je sélectionne une langue étrangère',
])
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'title',
'placeholder' => 'Je sélectionne un type de sortie ou d\'activité',
])
->add('start', DateTimeType::class, [
'widget' => 'choice'
])
->add('end', TimeType::class, [
'input' => 'datetime',
'widget' => 'choice',
])
->add('address', EntityType::class, [
'class' => Location::class,
'choice_label' => 'address',
'placeholder' => 'Je sélectionne une adresse',
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'save'],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Event::class,
'translation_domain' => 'forms'
]);
}
}
位置.php
<?php
namespace App\Entity;
use App\Repository\LocationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: LocationRepository::class)]
class Location
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $number = null;
#[ORM\Column(length: 255)]
private ?string $street = null;
#[ORM\Column(length: 255)]
private ?string $zipcode = null;
#[ORM\Column(length: 255)]
private ?string $city = null;
#[ORM\ManyToOne(inversedBy: 'locations')]
private ?BigCity $bigcity = null;
#[ORM\OneToMany(mappedBy: 'address', targetEntity: Event::class)]
private Collection $events;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 7, nullable: true)]
private ?string $lat = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 7, nullable: true)]
private ?string $lon = null;
#[ORM\ManyToOne(inversedBy: 'locations')]
private ?User $organizer = null;
public function __construct()
{
$this->events = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getNameAndId(): ?string
{
$nameandid =$this->getName() . ' (Id : ' . $this->getId() . ')';
return $nameandid;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(string $number): self
{
$this->number = $number;
return $this;
}
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(string $street): self
{
$this->street = $street;
return $this;
}
public function getZipcode(): ?string
{
return $this->zipcode;
}
public function setZipcode(string $zipcode): self
{
$this->zipcode = $zipcode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getAddress(): ?string
{
$address =$this->getName() . ', ' . $this->getNumber() . ' ' . $this->getStreet() . ', ' . $this->getCity();
return $address;
}
public function getBigcity(): ?BigCity
{
return $this->bigcity;
}
public function setBigcity(?BigCity $bigcity): self
{
$this->bigcity = $bigcity;
return $this;
}
/**
* @return Collection<int, Event>
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events->add($event);
$event->setAddress($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->removeElement($event)) {
// set the owning side to null (unless already changed)
if ($event->getAddress() === $this) {
$event->setAddress(null);
}
}
return $this;
}
public function getLat(): ?string
{
return $this->lat;
}
public function setLat(?string $lat): self
{
$this->lat = $lat;
return $this;
}
public function getLon(): ?string
{
return $this->lon;
}
public function setLon(?string $lon): self
{
$this->lon = $lon;
return $this;
}
public function getOrganizer(): ?User
{
return $this->organizer;
}
public function setOrganizer(?User $organizer): self
{
$this->organizer = $organizer;
return $this;
}
}
uj5u.com熱心網友回復:
這是解決方案:
型別.php 檔案
$user = $options['user'];
$builder
->add('address', EntityType::class, [
'class' => Location::class,
'query_builder' => function (EntityRepository $er) use ($user) {
return $er
->createQueryBuilder('l')
->andWhere('l.organizer = :user')
->setParameter('user', $user);
},
'choice_label' => 'address',
'placeholder' => 'Je sélectionne une adresse',
])
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Event::class,
'user' => User::class
]);
}
在 Controller.php 中指定用戶
$form = $this->createForm(EventType::class, $event, ['user' => $user]);
uj5u.com熱心網友回復:
這可以通過不同的方式實作
選項 1 來自 EventController.php 的過濾選項串列
$form = $this->createForm(EventType::class, $event, [
'addresses' => $locationRepository->findBy(['organizer' => $user])
]);
更多資訊 https://symfony.com/doc/current/forms.html#passing-options-to-forms
并在 EventType.php 中的 EntityType 中的選擇串列
->add('address', EntityType::class, [
'class' => Location::class,
'choice_label' => 'address',
'choices' => $options['addresses'],
])
更多資訊 https://symfony.com/doc/current/reference/forms/types/entity.html#choices
選項2 用戶的轉移,如您自己在上面已經描述的
事件控制器.php
$form = $this->createForm(EventType::class, $event, ['user' => $user]);
并使用 query_builder 選項構建查詢
事件型別.php
$user = $options['user'];
$builder
...
->add('address', EntityType::class, [
...
'query_builder' => function (EntityRepository $er) use ($user) {
$qb = $er->createQueryBuilder('l');
return $er
->andWhere(
$qb->expr()->eq('l.user', ':user')
)
->setParameter('user', $user)
;
},
])
...
;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524379.html
標籤:形式班级交响乐实体
