【发布时间】:2017-06-22 11:53:43
【问题描述】:
在 ZF2 - PostgreSQL 应用程序中,我想使用 Doctrine2 原生查询来构建分页器列表。
因此,如果选择任何自定义 Doctrine / Pgsql 类型,它会非常有用。但对于一个查询,我会使用自定义类型的数据。
我在 PostgreSQL 中声明了一个名为 AlertRecipient 的 Doctrine 2 自定义类型,如下所示:
CREATE TYPE alert_recipient AS (
email text,
status int
);
此类型在某些表格中使用。 clients 表中的示例:
ID (int) | name (varchar) | alerts (alert_recipients[])
1 | John Doe | {"(john@doe.com, 1), (jane@doe.com, 1)"}
2 | Foo Bar | {"(foo@bar.com, 1)"}
(alert_recipient[] 扩展 alert_recipient 以存储许多 alert_recipientrecords 的列表)
这种类型被链接到一个实体,用于水合:
class AlertRecipient
{
protected $email;
protected $status;
// ... with accessors
}
在onBoostrap事件上进行Doctrine类型注册:
// ...
if (!Type::hasType('alert_recipient'))
{
Type::addType('alert_recipient', AlertRecipient::class);
}
$platform->registerDoctrineTypeMapping('alert_recipient', 'alert_recipient');
if (!Type::hasType('alert_recipient[]'))
{
Type::addType('alert_recipient[]', AlertRecipients::class);
}
$platform->registerDoctrineTypeMapping('_alert_recipient', 'alert_recipient[]');
// ...
自定义类型学说适配器已经写好如doc http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html
查询看起来像:
$rsm = new ResultSetMappingBuilder($em);
$rsm->addRootEntityFromClassMetadata(Client::class, 'c');
// ... some other data from join entity (e.g)
$query = 'SELECT c.* FROM clients c JOIN ...';
$em->createNativeQuery($query, $rsm);
$results = $query->getResult(NativeQuery::HYDRATE_ARRAY);
问题是我没有来自 find() Doctrine 本地方法的相同行为,或者来自我的本地查询结果的水合。
在自定义类型适配器中调试:
public function convertToPHPValue($value, AbstractPlatform $platform)
{
var_dump($value); exit;
// ...
}
编辑
public function convertToPHPValueSQL($sqlExpr, $platform)
{
return 'to_json(' . $sqlExpr . ')';
}
来自find(),使用AlertRecipient 实体正确补水结果:string '[{"email":"john@doe.com","status":1}, {"email":"jane@doe.com","status":1}]'
从本机查询水合来看,AlertRecipient 实体中的 result['order_emails'] 没有水合:string '{"(john@doe.com, 1), (jane@doe.com, 1)"}'
那么数据没有被正确的水合......
感谢您的想法
编辑
在Client实体中声明alertRecipient属性:
/* @ORM\Column(type="alert_recipient[]", nullable=true, name="alert_recipients")
* @Gedmo\Versioned
*/
protected $alertRecipients = [];
// ... with accessors
【问题讨论】:
标签: php postgresql symfony doctrine-orm zend-framework2