So I'm trying to create an input array for my form, but haven't quite found any resource in the docs for this (I could have missed something so link if the case).
In short, I'd like to render something like this:
<select name="my-select" id="some-select">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<button type="button" class="btn btn-primary" id="add-new-option">
Add Another Option
</button>
so the idea is user can select Option A, hit the button to add Option C as well as A to the entity.
The option results get stored in a table like this:
entity_id option_value
1 A
1 C
2 B
2 A
I thought of just doing it like this:
foreach ($arrayInput as $key => $value)
{
$option = new Option();
$option->setEntityId($entity->getId());
$option->setOptionValue($value);
}
in the controller.
But I have a horrible feeling there's already a way to do this, I just haven't been able to find it. Otherwise I have to use HTML in my twig templates and I'm trying to keep that reduced..
So is the above method the best way to achieve what I want?
My current entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PageRepository")
*/
class Page
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=125)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="boolean")
*/
private $approved;
/**
* @ORM\Column(type="datetime")
*/
private $created_ts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_edit_ts;
/**
* @ORM\Column(type="integer")
*/
private $author_id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Tag")
*/
private $tag;
public function getTag(): ?Tag
{
return $this->tag;
}
public function getId()
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): self
{
$this->approved = $approved;
return $this;
}
public function getCreatedTs(): ?\DateTimeInterface
{
return $this->created_ts;
}
public function setCreatedTs(\DateTimeInterface $created_ts): self
{
$this->created_ts = $created_ts;
return $this;
}
public function getLastEditTs(): ?\DateTimeInterface
{
return $this->last_edit_ts;
}
public function setLastEditTs(?\DateTimeInterface $last_edit_ts): self
{
$this->last_edit_ts = $last_edit_ts;
return $this;
}
public function getAuthorId(): ?int
{
return $this->author_id;
}
public function setAuthorId(int $author_id): self
{
$this->author_id = $author_id;
return $this;
}
}
Thanks
EntityType- would that not be counter-intuative if they didn't want to mix entities with forms? And I generated the class withmake:entityand to me, database field should be snake_case rather than camelCaseEntityTypeso less techy people can prototype more quickly, I don't wish to discuss this further. Just because a Singleton pattern exists, that doesn't mean you should use it 2) Database properties sure, but not entity propertiesmake:entityso it auto gens my class properties from the database properties