RFC - View models as improvement for partial models. #6702

Closed
opened 2026-01-22 15:37:18 +01:00 by admin · 2 comments
Owner

Originally created by @scyzoryck on GitHub (Apr 25, 2021).

Hello!

In most of the application that were using tables usually has some fields that are used very rare - for example: User entity contains data about user password / preferences / bio / etc, but in most cases we need only base fields like: username, id or avatar. The solution would be to use partial models in DQL, but it would hydrate data to the same class - what may lead to issues.
What do you think to introduce another type of mapping that would hydrate partial model into another class?

Main rules under it could be:

  • it inherits mapping from parent entity, but it does't contains all of data from parent
  • it cannot be used to update fields in parent entity (limit complexity and issues)
  • it has its own repository with similar query method as normal entity
  • it can be used as a reference to parent entity (optional)

Usage

Mapping:

<?php
// src/User.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;
    /**
     * @ORM\Column(type="string")
     */
    protected $username;
    /**
     * @ORM\Column(type="string")
     */
    protected $password;
    /**
     * @ORM\Column(type="text")
     */
    protected $bio;
    /**
     * @ORM\Column(type="json")
     */
    protected $roles;

    // .. (other code)
}
<?php
// src/UserViewModel.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\ViewModel(parent="User")
 */
class UserViewModel
{
    /**
     * @ORM\ColumnView()
     */
    public $id;
    /**
     * @ORM\ColumnView()
     */
    public $username;

    protected $sampleField = 'I\'m not mapped to db';

    // .. (other code)
}

Repository usage:

$userView = $entityManager->getRepository('UserViewModel')->findOneBy(array('username' => 'scyzoryck'));

Benefits

  • easy and structured way to use partial models
  • less transfer and query parsing time on database side
  • it might optimise performance (no need to track such objects in UnitOfWork).

Just idea, I'm not sure if it would be useful or terrible to use so I'm really open for any comment :)

Best, Marcin!

Originally created by @scyzoryck on GitHub (Apr 25, 2021). Hello! In most of the application that were using tables usually has some fields that are used very rare - for example: User entity contains data about user password / preferences / bio / etc, but in most cases we need only base fields like: username, id or avatar. The solution would be to use partial models in DQL, but it would hydrate data to the same class - what may lead to issues. What do you think to introduce another type of mapping that would hydrate partial model into another class? Main rules under it could be: - it inherits mapping from parent entity, but it does't contains all of data from parent - it cannot be used to update fields in parent entity (limit complexity and issues) - it has its own repository with similar query method as normal entity - it can be used as a reference to parent entity (optional) #### Usage Mapping: ```php <?php // src/User.php use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="users") */ class User { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue */ protected $id; /** * @ORM\Column(type="string") */ protected $username; /** * @ORM\Column(type="string") */ protected $password; /** * @ORM\Column(type="text") */ protected $bio; /** * @ORM\Column(type="json") */ protected $roles; // .. (other code) } ``` ```php <?php // src/UserViewModel.php use Doctrine\ORM\Mapping as ORM; /** * @ORM\ViewModel(parent="User") */ class UserViewModel { /** * @ORM\ColumnView() */ public $id; /** * @ORM\ColumnView() */ public $username; protected $sampleField = 'I\'m not mapped to db'; // .. (other code) } ``` Repository usage: ```php $userView = $entityManager->getRepository('UserViewModel')->findOneBy(array('username' => 'scyzoryck')); ``` #### Benefits - easy and structured way to use partial models - less transfer and query parsing time on database side - it might optimise performance (no need to track such objects in UnitOfWork). Just idea, I'm not sure if it would be useful or terrible to use so I'm really open for any comment :) Best, Marcin!
admin closed this issue 2026-01-22 15:37:18 +01:00
Author
Owner

@beberlei commented on GitHub (Apr 25, 2021):

Have you seen the 'select new UserViewModel(u.username) from User u' syntax in DQL? I believe it covers your usecase

@beberlei commented on GitHub (Apr 25, 2021): Have you seen the 'select new UserViewModel(u.username) from User u' syntax in DQL? I believe it covers your usecase
Author
Owner

@scyzoryck commented on GitHub (Apr 26, 2021):

Totally forgot about it! Thanks for help! :)

@scyzoryck commented on GitHub (Apr 26, 2021): Totally forgot about it! Thanks for help! :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6702