zend 2 + Doctrine 2返回复选框值

时间:2013-10-05 11:34:01

标签: forms checkbox doctrine zend-framework2

我正在使用zend 2和doctrine 2

我不清楚如何使用复选框中的返回值返回和填充数据库。这些值应返回表的多个条目。

我找不到任何关于如何呈现或使用复选框的文档/文章;所以,我非常感谢您的建议或示例代码。

我在下面附上了我的课程。我相信问题发生在我的实体类中:即我没有正确使用getter / setter进行数组集合,因此我不断从我返回的postform获取错误消息,我的返回值为空。

这是我的代码:

我的mysql表:

CREATE TABLE workers_timetable(
id MEDIUMINT UNSIGNED NOT NULL, 
timesId MEDIUMINT UNSIGNED NOT NULL,
INDEX (id ,timesId ), 
INDEX times_id (timesId, id ) 

); 

我的班级;

<?php

namespace Workers\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface; 

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;


/**
 *
 * @ORM\Entity
 * @ORM\Table(name="workers_timetable")
 * @property int $timesId
 */
class WorkersAvailabilityTimeTable {

   /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer");
     */
    protected $timesId;




    public function __construct()
    {
        $this->timesId = new ArrayCollection();
    }


   public function getId()
    {
        return $this->id;
    } 



   public function addTimesId(Collection $timesId)
    {
        foreach ($timesId as $timesId) {
            $this->setTimesId($this);
            $this->timesId->add($timesId);
        }
    }

    public function setTimesId()
    {
        return $this->timesId;
    }


    public function removeTimesId(Collection $timesId)
    {
        foreach ($timesId as $timesId) {
            $this->setTimesId(null);
            $this->timesId->removeElement($timesId);
        }
    }

    public function getTimesId()
    {
        return $this->timesId;
    }


    //put your code here
}

我的fieldSet类

<?php

namespace Workers\Form\Fieldset;

use Workers\Entity\WorkersAvailabilityTimeTable;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;



class WorkersAvailabilityTimeTableFieldset extends Fieldset implements InputFilterProviderInterface
{
   public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('WorkerTimeTable');


        $this->setHydrator(new DoctrineHydrator($objectManager, 'Workers\Entity\WorkersAvailabilityTimeTable'))
             ->setObject(new WorkersAvailabilityTimeTable());


        $this->add(array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Hidden',
        ));

         $this->add(array(
            'type' => 'Zend\Form\Element\MultiCheckbox',
            'name' => 'timesIds',
            'options' => array(
                'label' => 'Please Select Your Availablity',
                'value_options' => array(

                    '1'  =>'mon',
                    '2' =>'tues'
   ),
            ),
            'attributes' => array(
                'value' => '1' //set checked to '1'
            )
        ));
     )

1 个答案:

答案 0 :(得分:0)

首先,您要使用DoctrineModule\Form\Element\ObjectMultiCheckbox。有关详细信息,请参阅the DoctrineModule\docs

如果我现在不是完全愚蠢的话,那么第二件事就是你在你的实体内做错了什么......

public function addTimesId(Collection $timesId)
{
    foreach ($timesId as $timesId) {
        $this->setTimesId($this);      // This should be $timesId->setWorker($this);
        $this->timesId->add($timesId);
    }
}

再次在the DoctrineModule\docs

查看有关后者的更多信息