我在教义实体中有一个名为"birthday"的字段.
我想创建一个使用doctrine添加到数据库的对象.
控制器内部:
$name = "John Alex";
$birthday = "11-11-90";
$student = new Student();
$student->setName($name);
$student->setBirthday(strtotime($birthday);
...
Run Code Online (Sandbox Code Playgroud)
但是当我试图坚持下去时,我得到了这个错误
Fatal error: Call to a member function format() on a non-object in /Library/WebServer/Documents/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
Run Code Online (Sandbox Code Playgroud)
编辑:
我的实体:
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var date $birthday
*
* @ORM\Column(name="birthday", type="date", nullable=true)
*/
private $birthday;
/**
* Set birthday
*
* @param date $birthday
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
} …Run Code Online (Sandbox Code Playgroud) 是否可以比较当前"脏"版本(具有某些属性已更改但尚未保留的对象)与"原始"版本(数据仍在数据库中)之间的实体对象的状态.
我的假设是我可能有一个"脏"的对象,然后从数据库中提取一个新的对象并比较两者.例如:
$entity = $em->getRepository('MyContentBundle:DynamicContent')->find($id);
$editForm = $this->createContentForm($entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$db_entity = $em->getRepository('MyContentBundle:DynamicContent')->find($id);
// compare $entity to $db_entity
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('content_edit', array('id' => $id)));
}
Run Code Online (Sandbox Code Playgroud)
但根据我的经验,$ entity和$ db_entity始终是相同的对象(并且在$ request bind形式之后具有与$ entity相同的数据).有没有办法得到一个新的版本的$实体和"脏"版本为了比较的缘故?我见过的解决方案都会在表单绑定发生之前提取所需的数据,但我宁愿没有这个限制.
更新:为了澄清,我不仅要查看实体属性的更改,还要查看其相关的实体集合.