我目前正在与Symfony2合作开展一个小项目。
我正在尝试从表格中获取所有条目(默认情况下为今天)。
我首先在我的doctrine查询构建器中尝试使用DAY()
,但这不起作用,所以我现在在我的实体存储库中使用本机sql查询:
$em = $this->getEntityManager();
$rsm = new ResultSetMappingBuilder($em);
$rsm->addRootEntityFromClassMetadata('Teacup\CompareBundle\Entity\Log', 'l');
$rsm->addJoinedEntityFromClassMetadata('Teacup\CompareBundle\Entity\Frame',
'f',
'l',
'frame',
array('id' => 'frame_id'));
$query = $em->createNativeQuery('SELECT l.id, l.severity,
l.details, l.created_at, f.url
FROM log l
INNER JOIN frame f ON l.frame_id = f.id
WHERE DAY(l.created_at) = :day', $rsm);
但是我收到了这个错误:
The column 'created_at' conflicts with another column in the mapper.
我找不到如何解决它。我很确定这不过是,我只是继续努力而没有任何成功。有人能帮助我吗?
答案 0 :(得分:1)
为什么不使用DBAL图层?
$day = date('d');
$dbal = $this->getDoctrine()->getConnection('default');
$stmt = 'SELECT l.id, l.severity, l.details, l.created_at, f.url FROM log l
INNER JOIN frame f ON l.frame_id = f.id
WHERE DAY(l.created_at) = :day';
$data = $dbal->fetchAssoc( $stmt , array( 'day' => $day ) );
答案 1 :(得分:1)
指定Doctrine的重命名模式以自动解决冲突:
$rsm = new ResultSetMappingBuilder($em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
通常配置RSM,然后为您的查询生成select cluse:
$selectClause = $rsm->generateSelectClause()
$query = $em->createNativeQuery('SELECT ' . $selectClause. '
FROM log l
INNER JOIN frame f ON l.frame_id = f.id
WHERE DAY(l.created_at) = :day', $rsm);