我正在Lamp Server中使用Zend Framework 2 + Doctrine开发一个页面。我很难解决这个烦人的问题。我有一个实体:
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Usuarios
*
* @ORM\Table(name="usuarios", indexes= {@ORM\Index(name="fk_usuarios_idTipo_idx", columns={"idTipo"})})
* @ORM\Entity
*/
class Usuarios
{
/**
* @var \DateTime
*
* @ORM\Column(name="dataCriacao", type="datetime", nullable=false)
*/
private $datacriacao;
/**
* Set datacriacao
*
* @param \DateTime $datacriacao
*
* @return Usuarios
*/
public function setDatacriacao($datacriacao)
{
$this->datacriacao = $datacriacao;
return $this;
}
/**
* Get datacriacao
*
* @return \DateTime
*/
public function getDatacriacao()
{
return $this->datacriacao;
}
public function exchangeArray($data = array())
{
$this->datacriacao = isset($data['datacriacao']) ? $data['datacriacao'] : null;
}
}
它基本上是一个用户实体,所以我删除了不必要的代码。
在UsuariosForm中:
<?php
namespace Application\Form\Usuarios;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\InputFilter\InputFilter;
class UsuariosForm extends Form
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('Usuarios');
$this->setAttribute('enctype', 'multipart/form-data');
$this->setAttribute('method', 'post');
$this->setHydrator(new DoctrineHydrator($objectManager, 'Application\Entity\Usuarios'));
$this->add(array(
'type' => 'Zend\Form\Element\DateTime',
'name' => 'datacriacao',
// 'required' => false,
'options' => array(
'label' => 'Created At',
'format' => 'Y-m-d H:i:s'
),
'attributes' => array(
'class' => 'form-control input-sm',
// 'min' => Date('Y-m-d H:i:s'),
// 'max' => strtotime('+1 day', Date('Y-m-d H:i:s')),
// 'step' => 1,
)
)
);
我尝试过使用和不使用min,max和step datacriacao。插入记录就可以了。这是我更新记录的行动:
<?php
/**
* Action to recover access.
*
* @access public
*/
public function recoverAccessAction()
{
$recoverKey = $this->params('id', 0);
$entityManager = $this->getEntityManager();
$form = new UsuariosForm($entityManager);
$form->get('submit')->setAttribute('value', 'Submit');
$usuariosRepository = $entityManager->getRepository('Application\Entity\Usuarios');
$usuario = $usuariosRepository->findOneBy(array('senhastatus' => 2, 'resetsenha' => $recoverKey ));
$validationMessages = array();
if (isset($usuario))
{
if ($this->request->isPost())
{
$data = $this->request->getPost();
try {
$hydrator = new DoctrineHydrator($entityManager, 'Application\Entity\Usuarios');
$validationMessages = $this->checkPassword($data['senha'], $data['conferirSenha'], $usuario->getdatanasc()->format('Y-m-d'));
if (sizeof($validationMessages) == 0)
{
$data = $hydrator->extract($usuario);
$data['datanasc'] = $usuario->getdatanasc()->format('Y-m-d');
// $data['datacriacao'] = new \DateTime($usuario->getdatacriacao());
$form->setData($data);
var_dump($form->isValid());
if ($form->isValid())
{
$stringHelper = new StringHelper();
$salt = $stringHelper->generateRandomString();
$senha = md5($data['senha'].$usuario->getSalt());
$usuario->setSenha($senha);
$usuario->setdatacriacao(new \DateTime($usuario->getdatacriacao()));
$entityManager->persist($usuario);
$entityManager->flush();
}
else
print_r($form->getMessages());
}
else
{
// Exibindo todas as mensagens de erro 'anexadas' aos campos
foreach ($validationMessages as $key => $messages) {
foreach ($messages as $key => $message)
$form->setMessages(array($messages[0] => array($message) ));
}
print_r($form->getMessages());
}
} catch (Exception $e) {
// todo @gustavo: implement
}
}
}
return array('form' => $form, 'usuario' => $usuario);
}
进入插入操作,我只是这样做:
$usuario->setdatacriacao(new \DateTime('now'));
如果我必须设置任何值,那么这很简单:
$usuario->setdatacriacao($usuario->getdatacriacao()->format('Y-m-d H:i:s'));
修改
更新记录时,会发生这种情况:
Array ( [datacriacao] => Array ( [dateStepNotStep] => The input is not a valid step ) )
您可以注意到,在更新时我对此列没有任何作用。并且它不应该有任何改变:我只想更新一些列,而不是所有列! Sooo ......这里的主要问题是:为什么这个ZF2无法像日常一样轻松处理日期时间?如果插入工作正常,为什么更新它会给我这个噩梦呢?
编辑#2:
实际上,不再适用于插入。这是什么东西?
错误: 在/home/inhotim/Trabalho/0-OPE/scape/ProduçÃ00-o/Web/scape/vendor/zendframework/zend-validator/src/DateStep.php中调用非对象的成员函数format()在第220行
Value setted:
'datacriacao' =>
object(DateTime)[500]
public 'date' => string '2015-08-14 16:18:28' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Sao_Paulo' (length=17)
提前致谢
答案 0 :(得分:3)
我认为DateTime元素的默认值是1分钟步骤,它将它发送给验证器。因此,如果秒不为零,则会引发错误。尝试00秒并检查它是否消失。否则,您需要将步骤选项设置为“任何”。