Can I use ResultSetMapping-> addEntityResult () with an object that doesn't have a database table?

Getting this error:

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

      

When I use this:

$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 does not have a database table and that I would like to keep it for normalization purposes (do not store calculated values). Can ResultSetMapping be used with an object that doesn't have a database table? DTO, so to speak?

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;

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

    ......

      

+3


source to share


1 answer


Indeed, it looks like ResultSetMapping can only be used with existing entities. Since Doctrine 2.4 you can use any PHP / POPO class using the "NEW" statement syntax :

<?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

      



You can specify any PHP class, it only requires the constructor of that class to match the NEW operator.

Feel free to join our discussion of these strategies with PHP and Domain Driven Design.

+2


source







All Articles