原则2标识符中的ORM DateTime字段 [英] Doctrine 2 ORM DateTime field in identifier

查看:137
本文介绍了原则2标识符中的ORM DateTime字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的数据库表中,我们的列 refID date 是一个复合主键,标识符被映射为 datetime

In our DB tables, we columns refID and date are a composite primary key, with one field of the identifier being mapped as a datetime:

class corpWalletJournal
{
    /**
     * @ORM\Column(name="refID", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $refID;

    /**
     * @ORM\Column(name="date", type="datetime", nullable=false)     
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $date;

    public function setRefID($refID)
    {
        $this->refID = $refID;
    }

    public function setDate(\DateTime $date)
    {
        $this->date = $date;
    }
}

如果我们将实体描述为@ ORM\ id代码将返回异常无法将datetime转换为字符串...

If we describe them in entity as @ORM\Id this code will return exception "cannot convert datetime to string"...

$filter = array(
    'date' => $this->stringToDate($loopData['date']), 
    'refID' => $loopData['refID']
));

$oCorpWJ = $this->em->getRepository('EveDataBundle:corpWalletJournal')->findOneBy($filter);
// ...
$oCorpWJ->setDate($this->stringToDate($loopData['date']));
// ...

如果我们描述 corpWalletJournal#date 作为一个简单的列,代码工作正常。为什么?

If we describe corpWalletJournal#date as a simple column, code works fine. Why?

我们如何处理?我们需要在主键中同时使用 date refID

How can we deal with it? We need to have both date and refID in the primary key.

ADDED:

所以我创建了新课程

use \DateTime;

class DateTimeEx extends DateTime
{

public function __toString()
{
    return $this->format('Y-m-d h:i:s');
}

}

它的新类型



And new type for it

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Eve\DataBundle\Entity\Type\DateTimeEx;

class DateTimeEx extends Type
{
    const DateTimeEx = 'datetime_ex';

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return 'my_datetime_ex';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return new DateTimeEx($value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return $value->format('Y-m-d h:i:s');
    }

    public function getName()
    {
        return self::DateTimeEx;
    }

    public function canRequireSQLConversion()
    {
        return true;
    }

}

我如何在实体中使用它们?

How can i use them in entity?

我的(编辑)类型类

    use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class DateTimeEx extends Type
{
    const DateTimeEx = 'datetime_ex';

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return 'my_datetime_ex';
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function getName()
    {
        return self::DateTimeEx;
    }

}


推荐答案

Doctrine 2 ORM需要将标识符字段转换为 的UnitOfWork 。要求 EntityManager 可以跟踪对象的更改。

Doctrine 2 ORM needs to convert identifier fields to strings in the UnitOfWork. This is required to have the EntityManager being able to track changes to your objects.

由于类型为 DateTime 的对象本来不实现方法 __ toString ,将其转换为字符串并不像投射一样简单

Since objects of type DateTime don't natively implement method __toString, converting them to strings is not as simple as casting them to strings.

因此,默认的日期 datetime 时间类型不支持作为标识符

Therefore, the default date, datetime and time types are not supported as part of the identifier.

要处理它,您应该定义自己的自定义字段类型 mydatetime 映射到您自己的 MyDateTime 实现 __ toString 的类。这样,如果ORM包含对象,ORM也可以处理标识符。

To deal with it, you should define your own custom field type mydatetime mapped to your own MyDateTime class which implements __toString. That way, the ORM can handle identifiers also if they contain objects.

下面是一个例子,说明该类可能如下所示:

Here's an example of how that class may look like:

class MyDateTime extends \DateTime 
{
    public function __toString()
    {
        return $this->format('U');
    }
}

这里有一个例子,说明自定义DBAL类型看起来像:

And here's an example of how the custom DBAL type would look like:

use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class MyDateTimeType extends DateTimeType
{
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        $dateTime = parent::convertToPHPValue($value, $platform);

        if ( ! $dateTime) {
            return $dateTime;
        }

        return new MyDateTime('@' . $dateTime->format('U'));
    }

    public function getName()
    {
        return 'mydatetime';
    }
}

然后在引导过程中注册ORM配置取决于您使用的框架)。在symfony中,它记录在 symfony教义文档中。

Then you register it with your ORM configuration during bootstrap (depends on the framework you are using). In symfony, it is documented on the symfony doctrine documentation.

之后,您可以在实体中使用它:

After that, you can use it in your entities:

class corpWalletJournal
{
    // ...

    /**
     * @ORM\Column(name="date", type="mydatetime", nullable=false)     
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $date;

这篇关于原则2标识符中的ORM DateTime字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆