Symfony / Doctrine:DateTime作为主键 [英] Symfony/Doctrine: DateTime as primary key
本文介绍了Symfony / Doctrine:DateTime作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使一个实体使用日期作为主键。问题是Symfony无法将我正在使用的DateTime转换为字符串,以便在IdentityMap中引入它。在实体持久化期间,我收到以下错误:
Catchable致命错误:DateTime类的对象无法转换为字符串我在实体中使用这个代码: / p>
/ **
* @ ORM\Id
* @ ORM\Column(type = datetime)
* /
protected $ date;
错误出现在实体存储库中:
$ em = $ this-> getEntityManager();
$ currentData = new CurrentData();
...
$ currentData-> setDate(new \DateTime($ dateStr));
...
$ em-> persist($ currentData);
$ em-> flush();
我该如何解决这个问题?谢谢你。
解决方案这个解决方案是实现你自己的DBAL类型,使用带有__toString()的DateTime后代:
<?php
class DateKey extends \DateTime {
function __toString(){
return $ this-> format('c');
}
静态函数fromDateTime(\DateTime $ dateTime){
return new static($ dateTime-> format('c'));
}
}
class DateKeyType extends \Doctrine\DBAL\Types\DateType {
public function convertToPHPValue($ value,\Doctrine\ DBAL\Platforms\AbstractPlatform $ platform){
$ value = parent :: convertToPHPValue($ value,$ platform);
if($ value!== NULL){
$ value = DateKey :: fromDateTime($ value);
}
return $ value;
}
public function getName()
{
return'DateKey';
}
}
\Doctrine\DBAL\Types\Type :: addType('datekey','DateKeyType');
//编辑:创建实体管理器后不要忘记这一点。
//否则,您将遇到与doctrine数据库差异/迁移有关的问题。
$ platform = $ entityManager-> getConnection() - > getDatabasePlatform();
$ platform-> registerDoctrineTypeMapping('datekey','datekey');
$ platform-> markDoctrineTypeCommented(\Doctrine\DBAL\Types\Type :: getType('datekey'));
I am trying to make an Entity using a date as a primary key. The problem is that Symfony can't convert the DateTime I'm using into a string to introduce it in the IdentityMap. I get the following error during the persist of the entity:
Catchable Fatal Error: Object of class DateTime could not be converted to string in..
I'm using this code in the entity:
/**
* @ORM\Id
* @ORM\Column(type="datetime")
*/
protected $date;
The error appears in the entity repository:
$em = $this->getEntityManager();
$currentData = new CurrentData();
...
$currentData->setDate(new \DateTime($dateStr));
...
$em->persist($currentData);
$em->flush();
How can I solve this problem? Thank you.
解决方案 A roubust solution to this is to implement your own DBAL type, using a DateTime descendant with __toString() implemented:
<?php
class DateKey extends \DateTime{
function __toString() {
return $this->format('c');
}
static function fromDateTime(\DateTime $dateTime) {
return new static($dateTime->format('c'));
}
}
class DateKeyType extends \Doctrine\DBAL\Types\DateType{
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) {
$value = parent::convertToPHPValue($value, $platform);
if ($value !== NULL) {
$value = DateKey::fromDateTime($value);
}
return $value;
}
public function getName()
{
return 'DateKey';
}
}
\Doctrine\DBAL\Types\Type::addType('datekey', 'DateKeyType');
//edit: do not forget this after creating entity manager.
//otherwise, you will get into problems with doctrine database diff / migrations.
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('datekey', 'datekey');
$platform->markDoctrineTypeCommented(\Doctrine\DBAL\Types\Type::getType('datekey'));
这篇关于Symfony / Doctrine:DateTime作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文