0

我正在使用 zend 2 和教义 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'
            )
        ));
     )
4

1 回答 1

0

首先,您要使用DoctrineModule\Form\Element\ObjectMultiCheckbox. 有关更多信息,请参阅DoctrineModule\docs

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

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

在DoctrineModule\docs上再次查看有关后者的更多信息

于 2013-10-05T11:55:58.743 回答