我可以将ResultSetMapping-> addEntityResult()与没有数据库表的实体一起使用吗?

时间:2014-10-18 15:31:34

标签: php symfony doctrine-orm

出现此错误:

Class "Bundle\LeagueStanding" is not a valid entity or mapped super class.

当我使用它时:

$sql = '......';

$rsm = new ResultSetMapping();
$rsm->addEntityResult('Bundle:LeagueStanding', 's');
$rsm->addFieldResult('s', 'played', 'played');
//........etc
$query = $this->_em->createNativeQuery($sql, $rsm);

return $query->getResult();

LeagueStanding没有数据库表格,这就是我希望它能够强制执行标准化(不存储计算值)的方式。是否仍然可以将ResultSetMapping与没有数据库表的实体一起使用?可以这么说DTO?

namespace Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Bundle\Entity\League;
use Bundle\Entity\Team;
use Bundle\Entity\Season;

class LeagueStanding
{
    private $position = 0;

    private $played = 0;

    private $homePlayed = 0;

    private $awayPlayed = 0;

    private $won = 0;

    private $homeWon = 0;

    private $awayWon = 0;

    private $lost = 0;

    .................

    ......

1 个答案:

答案 0 :(得分:2)

事实上,看起来ResultSetMapping只能与现有实体一起使用。 从Doctrine 2.4开始,可以使用“NEW” Operator Syntax

来使用任何PHP类/ POPO
<?php
class CustomerDTO
{
    public function __construct($name, $email, $city, $value = null)
    {
        // Bind values to the object properties.
    }
}

<?php
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, e.email, a.city) FROM Customer c JOIN c.email e JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
  

您可以指定任何PHP类,它只要求此类的构造函数与NEW语句匹配。

使用PHP和域驱动设计随意join our discussion on these strategies