url support in choices

This commit is contained in:
Baptiste Lemoine 2020-01-22 10:22:36 +01:00
parent 150dc50003
commit 9d4a20783d
2 changed files with 51 additions and 34 deletions

View File

@ -169,12 +169,13 @@ class DefaultController extends AbstractController {
$newpoll->setPassword( $data[ 'password' ] );
}
// manage choices
$choices = $data[ 'choices_to_create' ];
$choices = $data[ 'answers' ];
foreach ( $choices as $c ) {
$newChoice = new Choice();
$newChoice
->setPoll( $newpoll )
->setName( $c );
->setUrl( $c[ 'url' ] )
->setName( $c[ 'text' ] );
$em->persist( $newChoice );
}
$em->persist( $newpoll );

View File

@ -2,6 +2,8 @@
namespace App\Entity;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@ -27,53 +29,61 @@ class Choice {
* @Serializer\Expose()
*/
public $name;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $url;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Type("datetime")
*/
public $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
* @Serializer\Type("App\Entity\Vote")
*/
public $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"})
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
* @Serializer\Type("App\Entity\Vote")
*/
public $votes;
public function __construct( $optionalName = null ) {
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->setDateTime( new DateTime() );
if ( $optionalName ) {
$this->setName( $optionalName );
}
}
public function display() {
return [
'id' => $this->getId(),
'date' => $this->getDateTime(),
'text' => $this->getName(),
'url' => $this->getUrl(),
];
}
public function __construct( $optionalName = null ) {
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->setDateTime( new \DateTime() );
if ( $optionalName ) {
$this->setName( $optionalName );
}
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
public function getId(): ?int {
return $this->id;
}
public function getDateTime(): ?DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getName(): ?string {
return $this->name;
}
@ -84,20 +94,16 @@ class Choice {
return $this;
}
public function getDateTime(): ?\DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?\DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
/**
* @return Collection|Vote[]
*/
@ -125,4 +131,14 @@ class Choice {
return $this;
}
public function getUrl(): ?string {
return $this->url;
}
public function setUrl( ?string $url ): self {
$this->url = $url;
return $this;
}
}