首先,对不起我的英语。
我的订单实体
* @ORM\Entity
* @ORM\Table(name="orders")
class Order implements InputFilterAwareInterface
{
...
/**
* Order status
*
* @ORM\OneToOne(targetEntity="Admin\Entity\OrdersStatus")
* @ORM\JoinColumn(name="status_id", referencedColumnName="id")
**/
private $status;
...
}
我的订单的雕像实体
* @ORM\Entity
* @ORM\Table(name="orders_statuses")
class OrdersStatus implements InputFilterAwareInterface
{
/**
* @var Zend\InputFilter\InputFilter
*/
protected $inputFilter;
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $title;
}
我的订单
class OrderForm extends Form
{
public function __construct(EntityManager $entityManager)
{
parent::__construct('Order');
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'itemForm');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'name' => 'status',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'label' => 'Status',
'object_manager' => $entityManager,
'empty_option' => '--- Select status ---',
'target_class' => 'Admin\Entity\OrdersStatus',
'property' => 'title'
),
'attributes' => array(
'required' => false
)
));
}
}
我的订单控制器
class OrderController extends AbstractActionController
{
...
public function editAction()
{
$em = $this->getEntityManager();
$id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
$item = new Stock\Entity\Order;
if ($id) {
$item = $em->getRepository($this->entity)->find($id);
}
$form = new Stock\Form\OrderForm($em);
$form->setHydrator(new DoctrineEntity($em, Stock\Entity\Order, false));
$form->bind($item);
$request = $this->getRequest();
if ($request->isPost()) {
$post = $request->getPost();
$form->setInputFilter($item->getInputFilter());
$form->setData($post);
if ($form->isValid()) {
$em->persist($item);
$em->flush();
return $this->redirect()->toRoute('stock/order', array('action' => 'index'));
}
}
...
}
当我在不选择订单状态的情况下尝试验证表单时,出现以下错误:“值是必需的,不能为空”。
当我使用这个输入过滤器时:
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'client',
'required' => false,
)));
$inputFilter->add($factory->createInput(array(
'name' => 'status',
'allow_empty' => true,
'required' => false,
'filters' => array(
array('name' => 'Null'),
),
)));
}
}
我得到了这个:Admin\Entity\OrdersStatus 的查询缺少标识符 id
没有空过滤器:
执行“SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3 FROM orders_statuses t0 WHERE t0.id = ?”时发生异常 带参数 [""]:
SQLSTATE [22P02]:无效的文本表示:7 错误:整数的无效输入语法:“”
在订单状态中允许空值的正确方法是什么?