我用的是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'
)
));
)发布于 2013-10-05 11:55:58
首先,您需要使用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上查看更多关于晚些时候的信息。
https://stackoverflow.com/questions/19197329
复制相似问题