Luca Coppola
Luca Coppola

Reputation: 53

Symfony one to many with checkbox list in form view

I have two Entities

Employee and FreeDay

One employee can have one or more free days. The free days are defined as day of the week so Monday => 1, Tuesday => 2 etc..

In the Employee Form View I would like to show the FreeDay entity as checkbox list like this:

Days Form View

But I got this error:

Warning: spl_object_hash() expects parameter 1 to be object, integer given

I'm really confused and I don't know how to get out of this issue.

The entity code is the following:

Employee Entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * Employee
 *
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class Employee
{

/**
 * @ORM\OneToMany(targetEntity="FreeDay", mappedBy="employee", cascade={"persist","remove"},orphanRemoval=true)
 */
private $freedays;



/**
 * Constructor
 */
public function __construct()
{
    $this->freedays = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add freedays
 *
 * @param \AppBundle\Entity\Free $freeday
 *
 * @return Employee
 */
public function addFreeDay(\AppBundle\Entity\FreeDay $freeday)
{
    $this->freedays[] = $freeday;

    return $this;
}

/**
 * Remove freeday
 *
 * @param \AppBundle\Entity\FreeDay $freeday
 */
public function removeFreeDay(\AppBundle\Entity\FreeDay $freeday)
{
    $this->freedays->removeElement($freeday);
}

/**
 * Get freeday
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getFreeDays()
{
    return $this->freedays;
}
}

Free Day Entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

class FreeDay
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var int
 * @ORM\ManyToOne(targetEntity="Employee", inversedBy="freedays")
 * @ORM\JoinColumn(name="employee_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $employee;

/**
 * @var int
 *
 * @ORM\Column(name="day", type="integer")
 */
private $day;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}


/**
 * Set day
 *
 * @param integer $day
 *
 * @return FreeDay
 */
public function setDay($day)
{
    $this->day = $day;

    return $this;
}

/**
 * Get day
 *
 * @return int
 */
public function getDay()
{
    return $this->day;
}

/**
 * Set employee
 *
 * @param \AppBundle\Entity\Employee $employee
 *
 * @return FreeDay
 */
public function setEmployee(\AppBundle\Entity\Employee $employee = null)
{
    $this->employee = $employee;

    return $this;
}

/**
 * Get employee
 *
 * @return \AppBundle\Entity\Employee
 */
public function getEmployee()
{
    return $this->employee;
}

EmployeeType.php

class EmployeeType extends AbstractType
{


public function buildForm(FormBuilderInterface $builder, array $options)
{



    $builder

        ->add('freedays', EntityType::class, array(
            'class'=>'AppBundle:FreeDay',
            'choices'=>$this->getDays(),
            'expanded'=>true,
            'multiple'=>true
        ))

        ->add('save', SubmitType::class, array('label' => 'Salva'))
    ;
}


private function getDays() {
    return array(
        'Monday'=>1,
        'Tuesday'=>2,
        'Wednesday'=>3,
        'Thursday'=>4,
        'Friday'=>5,
        'Saturday'=>6,
    );
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Employee',
    ));
}
}

Upvotes: 1

Views: 1049

Answers (1)

Nick
Nick

Reputation: 101

Using the entity option in your form builder requires the options to be objects of that entity, see here in the symfony documentation: http://symfony.com/doc/current/reference/forms/types/entity.html#using-choices

as you're passing an array to it you will always come across this error.

to achieve what you outline in your question you will need to have the freedays in your database with the relevant id and name like you list in your getDays function, so Monday will have an id in your database of 1.

if you just want all the options see this section of the documentation: http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage

however if you want filter the results you will need a query builder, described here in the documentation: http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities

Please let me know if this doesn't work for you or I've misunderstood the question.

Upvotes: 3

Related Questions