Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сreate SelectNew query modifier for use NEW Operator Syntax #292

Merged
merged 1 commit into from
Feb 1, 2021

Conversation

peter-gribanov
Copy link
Member

NEW Operator Syntax

Using the NEW operator you can construct Data Transfer Objects (DTOs) directly from DQL queries.

  • When using SELECT NEW you don't need to specify a mapped entity.
  • You can specify any PHP class, it only requires that the constructor of this class matches the NEW statement.
  • This approach involves determining exactly which columns you really need, and instantiating a data-transfer object that contains a constructor with those arguments.
class CustomerDTO
{
    public function __construct($name, $email, $city, $value = null)
    {
        // Bind values to the object properties.
    }
}
// SELECT NEW CustomerDTO(customer.name, email.email, address.city, SUM(orders.value)) FROM Customer customer JOIN customer.email email JOIN customer.address address JOIN customer.orders orders GROUP BY customer.id
$spec = Spec::andX(
    Spec::selectNew(
        CustomerDTO::class,
        Spec::field('name'),
        Spec::field('email', 'email'),
        Spec::field('city', 'address'),
        Spec::SUM(Spec::field('value', 'orders')),
    ),
    Spec::groupBy('id'),
);

// array of CustomerDTO
$users = $rep->match($spec);

@peter-gribanov peter-gribanov self-assigned this Feb 1, 2021
@peter-gribanov peter-gribanov added this to the Release 2.0.0 milestone Feb 1, 2021
@peter-gribanov peter-gribanov mentioned this pull request Feb 1, 2021
37 tasks
@peter-gribanov peter-gribanov merged commit 14462ad into Happyr:2.0 Feb 1, 2021
@peter-gribanov peter-gribanov deleted the new_operator branch February 1, 2021 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant