PHP code example of pixelshaped / flat-mapper-bundle
1. Go to this page and download the library: Download pixelshaped/flat-mapper-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class CustomerDTO
{
public function __construct(
#[Identifier]
#[Scalar('customer_id')]
public int $id,
#[Scalar('customer_name')]
public string $name,
#[ScalarArray('shopping_list_id')]
public array $shoppingListIds
)
}
$result = $this->getOrCreateQueryBuilder()
->select('customer.id AS customer_id, customer.name AS customer_name, shopping_list.id AS shopping_list_id')
->leftJoin('customer.shopping_list', 'shopping_list')
->getQuery()->getResult()
;
$flatMapper = new \Pixelshaped\FlatMapperBundle\FlatMapper()
$flatMapper->map(CustomerDTO::class, $result);
$qb = $customerRepository->createQueryBuilder('customer');
$qb
->leftJoin('customer.addresses', 'customer_addresses')
->select('customer.id AS customer_id, customer.ref AS customer_ref, customer_addresses.id AS address_id')
->setFirstResult(0)
->setMaxResults(10)
;
$paginator = new Paginator($qb->getQuery(), fetchJoinCollection: true);
$paginator->setUseOutputWalkers(false);
$result = $flatMapper->map(CustomerWithAddressesDTO::class, $paginator);
class CustomerDTO
{
public function __construct($name, $email, $city, $value = null){ /* ... */ }
}
$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<CustomerDTO>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.