graefe/doctrine-extensions

Doctrine DBAL 和 ORM 的一些有用类型及其他小扩展的集合。

v0.3.0 2016-05-26 12:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:44 UTC


README

Build Status

这是一个 Doctrine DBAL 和 ORM 的有用类型及其他小扩展的集合。

UnixTimeType

UnixTimeType 是一种自定义的 Doctrine 映射类型,用于表示以 Unix 时间表示的时间戳值,即自 1970 年 1 月 1 日起的秒数。

// Register custom type during boot-strap.
\Doctrine\DBAL\Types\Type::addType('unixtime', '\Graefe\Doctrine\Type\UnixTimeType');

...

/**
 * @ORM\Column(name="created", type="unixtime", nullable=false)
 */
private $created;

MySqlEnumType

MySqlEnumType 是一个用于映射 MySQL 中 ENUM 类型的抽象基类。

// Define type.
class ShopModeType extends \Graefe\Doctrine\Type\MySqlEnumType
{
    protected function getValues() { return array('b2b','b2c'); }
    public function getName() { return 'shopmode'; }
}

...

// Register type during boot-strap.
\Doctrine\DBAL\Types\Type::addType('shopmode', 'ShopModeType');

...

/**
 * @ORM\Column(name="mode", type="shopmode", nullable=false)
 */
private $mode;

RAND()

Rand 类为 DQL 提供了 RAND() 函数以选择随机行。(注意:不当使用可能会引起严重的性能问题。)

// Register function.
$em->getConfiguration()->addCustomNumericFunction('RAND', '\\Graefe\\Doctrine\\Functions\\Rand');

...

$qb->select('d')
    ->addSelect('RAND() AS randval')
    ->from('Dummy', 'd')
    ->orderBy('randval')
    ->setMaxResults(1);