My doubt is about Api Platform. (https://api-platform.com)
I have two entities. Question and Answer. And I want to have a POST call to create a question with one answer. I show my entities.
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* #ApiResource(
* normalizationContext={"groups"={"question"}},
* denormalizationContext={"groups"={"question"}})
* #ORM\Entity
*/
class Question
{
/**
* #Groups({"question"})
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #Groups({"question"})
* #ORM\Column
* #Assert\NotBlank
*/
public $name = '';
/**
* #Groups({"question"})
* #ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;
public function getAnswers()
{
return $this->answers;
}
public function setAnswers($answers): void
{
$this->answers = $answers;
}
public function __construct() {
$this->answers = new ArrayCollection();
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getId(): int
{
return $this->id;
}
}
And a Answer Entity
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
*
* #ApiResource
* #ORM\Entity
*/
class Answer
{
/**
* #Groups({"question"})
* #ORM\Id
* #ORM\Column(type="guid")
*/
public $id;
/**
* #Groups({"question"})
* #ORM\Column
* #Assert\NotBlank
*/
public $name = '';
/**
* #ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
* #ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
public $question;
public function getQuestion()
{
return $this->question;
}
public function setQuestion($question): void
{
$this->question = $question;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getId(): string
{
return $this->id;
}
public function __toString()
{
return $this->getName();
}
}
Now I can create from dashboard of nelmio a question and into an answer. But in database, my answer doesnt have saved the relation with question.
{
"name": "my new question number 1",
"answers": [
{
"id": "ddb66b71-5523-4158-9aa3-2691cae9d473",
"name": "my answer 1 to question number 1"
}
]
}
And other question is... I've changed my id of answer by a guid, because I get and error when I create and answer into question without id. Can I create a question, and answers without to specify an id ?
Thanks in advance
Well, I see some errors in your entities... first since the relation between Question and Answer entity is a OneToMany, your Qestion entity should have this implemented, the:
use ApiPlatform\Core\Annotation\ApiProperty;
//..... the rest of your code
/**
* #ApiProperty(
* readableLink=true
* writableLink=true
* )
* #Groups({"question"})
* #ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;
public function __construct()
{
//....
$this->answers = new ArrayCollection();
//...
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this)
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
}
return $this;
}
the command
PHP bin/console make:entity
allows you to create a field in your entity of type relation and it creates these methods for you just follow the instructions (after both entities are created, use the command for update Question entity...)
the readableLink ApiProperty annotation is for see embedded object on GET request, is the same if you use serialization groups, if you set it on false then the response will look like this:
{
"name": "my new question number 1",
"answers": [
"/api/answers/1",
"/api/answers/2",
....
]
}
it is used to make responses smaller (among other things)... and the writableLink is for allowing POST request like this (see this example for more info here):
{
"name": "my new question number 1",
"answers": [
{
"id": "ddb66b71-5523-4158-9aa3-2691cae9d473",
"name": "my answer 1 to question number 1"
}
]
}
of course, using the corresponding serialization groups in each entity...
in ApiPlatform the embedded objects are persisted through setters and getters method but also add and remove methods for OneToMany relations, and the ORM does the rest of the work.
let me know if this helps. Cheers!
For the first point, it should persist in database without problem. Anyway, you could create a PostValidateSubscriber for Question entity and check if the relation is there.
<?php /** #noinspection PhpUnhandledExceptionInspection */
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class QuestionPostValidateSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
public function __construct(
TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
}
/**
* {#inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['checkQuestionData', EventPriorities::POST_VALIDATE]
];
}
/**
* #param GetResponseForControllerResultEvent $event
*/
public function checkQuestionData(GetResponseForControllerResultEvent $event)
{
$bid = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$question instanceof Question || (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method))
return;
$currentUser = $this->tokenStorage->getToken()->getUser();
if (!$currentUser instanceof User)
return;
}
}
And do an echo or use xdebug for check question.
For the second point, you can add these annotations for the id of entities, so the id's will generate theirself.
#ORM\GeneratedValue()
#ORM\Column(type="integer")
Related
I have a big problem with Symfony, Doctrine and partially incorrect data.
First of all
i use uuids for primary keys
i use jwts for authentication
i use symfony 5.4.x and doctrine-bundle 2.7.x
i use class inheriance for getting my pk on each entity
i donĀ“t use at all relations, because i want to have some flexibility to slice components into their origin e.g. security component as a microservice (but i think this is not really neccesarry on my question)
Sample of JWT content
{
"iat": xxx,
"exp": xxx,
"roles": [
"MY_ROLE"
],
"id": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
"tenant": {
"id": "11111111-2222-3333-4444-555555555555",
"name": "MyTenant"
},
"user": {
"id": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
"username": "MyUser",
"firstname": "My",
"name": "User"
}
}
Sample of Entity: AbstractEntity.php
<?php
namespace App\Entity;
use JsonSerializable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
/**
* Class AbstractEntity
* #package App\Entity
*
* #ORM\MappedSuperclass()
*/
abstract class AbstractEntity implements EntityInterface, JsonSerializable
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* #return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* #return bool
*/
public function isNew(): bool
{
return !isset($this->id);
}
/**
* #return array
*/
public function jsonSerialize(): array
{
return [
'id' => $this->getId()->toRfc4122()
];
}
/**
* #return Uuid
*/
public function __toString(): string
{
return $this->getId()->toRfc4122();
}
}
Sample of Entity: Settings.php
<?php
namespace App\Entity\Configuration;
use DateTime;
use App\Entity\AbstractEntity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
/**
* #ORM\Entity(repositoryClass="App\Repository\Configuration\SettingsRepository")
* #ORM\Table(name="configuration_settings", indexes={#ORM\Index(columns={"tenant"})})
*/
class Settings extends AbstractEntity
{
/**
* #var Uuid $tenant
*
* #ORM\Column(type="uuid")
*/
private Uuid $tenant;
/**
* #var string|null $payload
*
* #ORM\Column(type="json", nullable=true)
*/
private ?string $payload = "";
/**
* #return Uuid
*/
public function getTenant(): Uuid
{
return $this->tenant;
}
/**
* #param Uuid $tenant
*/
public function setTenant(Uuid $tenant): void
{
$this->tenant = $tenant;
}
/**
* #return string|null
*/
public function getPayload(): ?string
{
return $this->payload;
}
/**
* #param string|null $payload
*/
public function setPayload(?string $payload): void
{
$this->payload = $payload;
}
}
Sample of Controller: SettingsController.php
<?php
namespace App\Controller\Configuration\API;
use App\Entity\Configuration\Settings;
use App\Entity\Security\Role;
use App\Repository\Configuration\SettingsRepository;
use App\Service\Security\UserService;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Uid\Uuid;
/**
* Settings API controller.
*
* #Route("/api/configuration/settings", name="api_configuration_settings_")
*/
class SettingsController extends AbstractController
{
/**
* #var SettingsRepository
*/
private SettingsRepository $repository;
/**
* SettingsService constructor.
* #param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(Settings::class);
}
/**
* #Route("/obtain", name="obtain", methods={"GET"})
*/
public function obtain(UserService $userService, TokenStorageInterface $tokenStorageInterface, JWTTokenManagerInterface $jwtManager): JsonResponse
{
$userService->isGranted([
Role::ROLE_TENANT_USER
]);
$token = $jwtManager->decode($tokenStorageInterface->getToken());
$tenantAsRFC4122 = $token['tenant']['id']; // Here we have: 11111111-2222-3333-4444-555555555555
$tenant = Uuid::fromString($tenantAsRFC4122); // Here we have a class of Symfony\Component\Uid\UuidV6;
$settings = $this->repository->findOneBy([
'tenant' => $tenant
]);
return new JsonResponse(
$settings
);
}
}
If i now call the API-URL with the Authorization Header including the Bearer as JWT, i will get the Settings for my tenant 11111111-2222-3333-4444-555555555555. But this works not always ...
I can't describe it exactly, because I can't reproduce it, but on one of the systems with a lot of traffic it happens from time to time that despite the correct bearer, i.e. correct tenant, simply wrong settings for another tenant come back.
Is there a doctrine cache somewhere that might cache queries and assign them to the wrong repsonses at the end of the day?
SELECT payload FROM configuration_settings WHERE tenant = '0x1234567890123456789' // here doctrine automaticly strips it to hex
I am so perplexed, because the SQL behind it looks correct and right, here times as an example shortened.
Hope someone can help me.
Thanks,
S.
I have the following associations with Doctrine:
Charge:
id
amount
adjustmentItems
Adjustment
id
date
adjustmentItems
AdjustmentItem
id
adjustment
charge
amount
There are charges, and there are adjustments. Each adjustment is made up of adjustmentItems, which are adjustments to one or more charges.
When adding a new adjustment, I am adding adjustments, and the associated items, via deserialization. Ie:
$adjustment =
["date" => "2020-12-14",
"items" => [
["charge" => 84, "amount" => 600],
["charge" => 85, "amount" => 200],
]
];
Everything works well, except I validate the charges using Assert/Valid on the AdjustmentItem::charge.
In the charge validation, I check to make sure the sum of all the adjustments does not exceed the charge amount.
However, Charge::getAdjustmentItems() does not show the just created adjustmentItems (even though the adjustmentItem shows to charge, and persisting everything works as expected).
$adjustment->getAdjustmentItems()->first()->getCharge()->getAdjustmentItems()->toArray()
is:
[]
How can I get the Charge to "see" the items from deserialization before persist for validation?
SOURCE:
<?php
namespace App\Entity\TestFin;
use App\Repository\TestFin\ChargeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* #ORM\Entity(repositoryClass=ChargeRepository::class)
* #ORM\Table(name="`test_financials_charge`")
*/
class Charge
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="integer")
*/
private $amount;
/**
* #ORM\OneToMany(targetEntity=AdjustmentItem::class, mappedBy="charge")
*/
private $adjustmentItems;
public function __construct()
{
$this->adjustmentItems = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getAmount(): ?int
{
return $this->amount;
}
public function setAmount(int $amount): self
{
$this->amount = $amount;
return $this;
}
/**
* #return Collection|AdjustmentItem[]
*/
public function getAdjustmentItems(): Collection
{
return $this->adjustmentItems;
}
public function addAdjustmentItem(AdjustmentItem $adjustmentItem): self
{
if (!$this->adjustmentItems->contains($adjustmentItem)) {
$this->adjustmentItems[] = $adjustmentItem;
$adjustmentItem->setCharge($this);
}
return $this;
}
public function removeAdjustmentItem(AdjustmentItem $adjustmentItem): self
{
if ($this->adjustmentItems->contains($adjustmentItem)) {
$this->adjustmentItems->removeElement($adjustmentItem);
// set the owning side to null (unless already changed)
if ($adjustmentItem->getCharge() === $this) {
$adjustmentItem->setCharge(null);
}
}
return $this;
}
/**
* #Assert\Callback
*/
public function validateBalance(ExecutionContextInterface $context, $payload)
{
dd(count($this->adjustmentItems->toArray()));
if (1) {
$context->buildViolation('Charge balance (after adjustments) must be greater or equal to $0.')
->atPath('invoice')
->addViolation();
}
}
}
<?php
namespace App\Entity\TestFin;
use App\Repository\TestFin\AdjustmentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass=AdjustmentRepository::class)
* #ORM\Table(name="`test_financials_adjustment`")
*/
class Adjustment
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $note;
/**
* #ORM\OneToMany(targetEntity=AdjustmentItem::class, mappedBy="adjustment", orphanRemoval=true)
* #Assert\Valid
*/
private $adjustmentItems;
public function __construct()
{
$this->adjustmentItems = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
/**
* #return Collection|AdjustmentItem[]
*/
public function getAdjustmentItems(): Collection
{
return $this->adjustmentItems;
}
public function addAdjustmentItem(AdjustmentItem $adjustmentItem): self
{
if (!$this->adjustmentItems->contains($adjustmentItem)) {
$this->adjustmentItems[] = $adjustmentItem;
$adjustmentItem->setAdjustment($this);
}
return $this;
}
public function removeAdjustmentItem(AdjustmentItem $adjustmentItem): self
{
if ($this->adjustmentItems->contains($adjustmentItem)) {
$this->adjustmentItems->removeElement($adjustmentItem);
// set the owning side to null (unless already changed)
if ($adjustmentItem->getAdjustment() === $this) {
$adjustmentItem->setAdjustment(null);
}
}
return $this;
}
}
<?php
namespace App\Entity\TestFin;
use App\Repository\TestFin\AdjustmentItemRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass=AdjustmentItemRepository::class)
* #ORM\Table(name="`test_financials_adjustment_item`")
*/
class AdjustmentItem
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=Adjustment::class, inversedBy="adjustmentItems")
* #ORM\JoinColumn(nullable=false)
*/
private $adjustment;
/**
* #ORM\ManyToOne(targetEntity=Charge::class, inversedBy="adjustmentItems")
* #ORM\JoinColumn(nullable=false)
* #Assert\Valid
*/
private $charge;
public function getId(): ?int
{
return $this->id;
}
public function getAdjustment(): ?Adjustment
{
return $this->adjustment;
}
public function setAdjustment(?Adjustment $adjustment): self
{
$this->adjustment = $adjustment;
return $this;
}
public function getCharge(): ?Charge
{
return $this->charge;
}
public function setCharge(?Charge $charge): self
{
$this->charge = $charge;
return $this;
}
}
$json = json_encode([
"note" => "bla",
"adjustmentItems" => [
["charge" => 1],
],
]);
$credit = $this->_deserializeJson($json, Adjustment::class);
//dd($credit);
//dd($credit->getAdjustmentItems()->first()->getCharge()->getAdjustmentItems()->toArray());
$this->_validate($credit);
Calling Charge::addAdjustmentItem in AdjustmentItem::setCharge solved the issue for me.
Anyone know why this is needed?
public function setCharge(?Charge $charge): self
{
$this->charge = $charge;
// This is needed so that the Assert/Callback in Charge knows about this adjustmentItem.
$charge->addAdjustmentItem($this);
return $this;
}
Does EasyAdmin support entity classes with constructor arguments for properties that are meant to be not nullable? EasyAdmin instantiates the entity class even if you click the "Add " button, right? Unfortunatelly this results in an "Too few arguments to function __construct()" error. Do you have a solution for this problem?
I tend to use the constructor for entity properties that are not nullable. Unfortunatelly EasyAdmin throws errors like this one when I click on the e.g. Add FiscalYear button to create a new entity object (FiscalYear in my example):
Too few arguments to function App\Entity\FiscalYear::__construct(), 0 passed in /myProject/vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php on line 618 and exactly 2 expected
How can I prevent these errors? As you can see in the following entity class the two constructor arguments represent the data that is meant to be submitted via the form:
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\FiscalYearRepository")
*/
class FiscalYear
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\Column(type="integer")
*/
private int $title;
/**
* #ORM\Column(type="boolean", options={"default": 0})
*/
private bool $completed = false;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="fiscalYears")
* #ORM\JoinColumn(nullable=false)
*/
private Company $company;
public function __construct(int $title, Company $company)
{
$this->title = $title;
$this->company = $company;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): int
{
return $this->title;
}
public function setTitle(int $title): void
{
$this->title = $title;
}
public function getCompleted(): bool
{
return $this->completed;
}
public function setCompleted(bool $completed): void
{
$this->completed = $completed;
}
public function getCompany(): Company
{
return $this->company;
}
public function setCompany(Company $company): void
{
$this->company = $company;
}
}
Is there a possibility to let EasyAdmin show the "create a new entity object" form without instantiating the entity class?
No, EasyAdmin doesn't natively support constructor with argument.
To avoid this problem, you have three solution.
solution1: Override EasyAdminController
The documentation explains this method.
// src/Controller/AdminController.php
namespace App\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
class FiscalYearController extends EasyAdminController
{
public function createNewFiscalYearEntity()
{
//your own logic here to retrieve title and company
return new FiscalYear($title, $company);
}
}
Depending you business model, it could be very difficult to retrieve title and company
solution2: Respect the entity pattern and help your business model with a factory pattern
Your entities should respect the entity pattern and their constructor should be edited to remove arguments.
To replace your constructor in your business model, create a factory.
class FiscalYearFactory
{
public static function create(int $title, Company $company): FiscalYear
{
$fiscalYear = new FiscalYear();
$fiscalYear->setCompany($company);
$fiscalYear->setTitle($title);
return $fiscalYear;
}
}
in your model, you have to do some updates:
//Comment code like this in your business model
$fiscalYear = new FiscalYear(2020,$company);
//Replace it, by this code:
$fiscalYear = FiscalYearFactory::create(2020,$company);
Solution3 Accept null values in your constructor.
I do NOT like this solution. Your properties shall be edited too to accept null values, your getters shall be edited to return null value. This is a solution, but I discourage you to use it.
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\FiscalYearRepository")
*/
class FiscalYear
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* #ORM\Column(type="integer")
*/
private ?int $title;
/**
* #ORM\Column(type="boolean", options={"default": 0})
*/
private bool $completed = false;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="fiscalYears")
* #ORM\JoinColumn(nullable=false)
*/
private Company $company;
public function __construct(?int $title = null, ?Company $company = null)
{
$this->title = $title;
$this->company = $company;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?int
{
return $this->title;
}
You should use the first solution which is a better practice
I am trying to develop my first symfony web-app and I have decided to use the bundle EasyAdmin.
In this web-app, I would like to define the following model : an Event with several dates.
In order to create this, I have create 2 entities with the help of the symfony console : Event and EventDate:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\EventRepository")
*/
class Event
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="App\Entity\EventDate", mappedBy="event", orphanRemoval=true)
*/
private $dates;
public function __construct()
{
$this->dates = 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;
}
/**
* #return Collection|EventDate[]
*/
public function getDates(): Collection
{
return $this->dates;
}
public function addDate(EventDate $date): self
{
if (!$this->dates->contains($date)) {
$this->dates[] = $date;
$date->setEvent($this);
}
return $this;
}
public function removeDate(EventDate $date): self
{
if ($this->dates->contains($date)) {
$this->dates->removeElement($date);
// set the owning side to null (unless already changed)
if ($date->getEvent() === $this) {
$date->setEvent(null);
}
}
return $this;
}
public function __toString(): String
{
return $this->name;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\EventDateRepository")
*/
class EventDate
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $date;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="dates")
* #ORM\JoinColumn(nullable=false)
*/
private $event;
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getEvent(): ?Event
{
return $this->event;
}
public function setEvent(?Event $event): self
{
$this->event = $event;
return $this;
}
}
In order to be user-friendly, I would like to "customize" the form of an Event in order to allow the user to create in the same form the event and its dates.
In order to do this, I have define the Event entity's form like that:
easy_admin:
entities:
Event:
class: App\Entity\Event
form:
fields:
- {property: 'name'}
- {property: 'dates', type: 'collection'}
The render of the collection is right because I can add or remove a date:
But As you can see, the field that represent the EventDate is an edit text. I think it's because the field represent the EventDate class and not only the date attribute.
The aim is to have the date selector that I have if I add a new EventDate in EasyAdmin:
So the question is: How to custom EasyAdmin in order to add an Event and its dates in a single form?
Thank you for your help!
I found the way to do it.
I need to modify my yaml EasyAdmin file in order to introduce an entry_type:
easy_admin:
entities:
Event:
class: App\Entity\Event
form:
fields:
- {property: 'name'}
- {property: 'dates', type: 'collection', type_options: {entry_type: 'App\Form\EventDateForm', by_reference: false}}
Then, I have to create the EventDateForm class:
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class EventDateForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\EventDate'
));
}
}
I also need to update the $date attribut of my Event entity like this:
/**
* #ORM\OneToMany(targetEntity="App\Entity\EventDate", mappedBy="event", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $dates;
The render is not very beautiful but it works:
I'm trying to use the DoctrineBehaviors translatable extension in Symfony 4.Just setup a test following the documentation example:
translatable entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Entity(repositoryClass="App\Repository\FAQRepository")
*/
class FAQ
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/.**
* #ORM\Column(type="datetime", nullable=true)
*/
protected $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}
translation entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Entity
*/
class FAQTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #ORM\Column(type="text")
*/
protected $question;
/**
* #ORM\Column(type="text")
*/
protected $answer;
/**
* #ORM\Column(type="integer", nullable=true)
*/
protected $category;
public function getQuestion(): ?string
{
return $this->question;
}
public function setQuestion(string $question): self
{
$this->question = $question;
return $this;
}
public function getAnswer(): ?string
{
return $this->answer;
}
public function setAnswer(string $answer): self
{
$this->answer = $answer;
return $this;
}
public function getCategory(): ?int
{
return $this->category;
}
public function setCategory(?int $category): self
{
$this->category = $category;
return $this;
}
}
Testing the translatable entity:
/**
* #Route("/test", name="test")
*/
public function testfaq()
{
$em = $this->getDoctrine()->getManager();
$faq = new FAQ();
$faq->translate('fr')->setQuestion('Quelle est la couleur ?');
$faq->translate('en')->setQuestion('What is the color ?');
$em->persist($faq);
$faq->mergeNewTranslations();
$em->flush();
return $this->render('app/test.html.twig', [
]);
}
A new ID is added in the faq table.
But nothing is persisted in the faqtranslation table.
Bundles.php :
Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle::class => ['all' => true],
All the documentations I found seem to refer to Symfony 3 or even Symfony 2, is it possible to use DoctrineBehaviors translatable in Symfony 4 ?
I don't know if you found your answer since (I hope you did), but yes you can use KnpLabs/DoctrineBehaviors with Symfony 4. Maybe, you just needed to wait a little longer for an update.