简体   繁体   中英

Doctrine - mapping to a non entity class

Is there a possibility to map doctrine's native query to a class that is not annotated as ORM entity? I have a basic class under App\Model

class BasicModel
{
    private int $weight;
    private int $points;
}

And my goal is to map the result of a query directly into this non entity class.

$rsm = new ResultSetMapping();
$rsm->addEntityResult(BasicModel::class, 'b');
$rsm->addFieldResult('b', 'points', 'points');
$rsm->addFieldResult('b', 'weight', 'weight');
$query = $this->em->createNativeQuery('select points, weight from some_table', $rsm);
$result = $query->getResult();

Currently, an error occurs

The class 'App\Model\BasicModel' was not found in the chain configured namespaces App\Entity

I don't want to annotate this class as Entity, since I don't need this information to be stored in database.

My current stack:

  • Symfony 5
  • MySQL 8
  • Doctrine 2

Unfortunately that is not possible. Doctrine is build as a Database Abstraction Layer (DBAL) and does not allow for this use case.

I see two paths forward. I know you say you do not want that, but since you have MySQL in your stack, you could opt to add the data anyway to the database and have the entity as a proper entity class. The other option is to not use Doctrine and build a different implementation to list all entities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM