PHP code example of danilovl / doctrine-entity-dto-bundle

1. Go to this page and download the library: Download danilovl/doctrine-entity-dto-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/ */

    

danilovl / doctrine-entity-dto-bundle example snippets



// config/bundles.php

return [
    // ...
    Danilovl\DoctrineEntityDtoBundle\DoctrineEntityDtoBundle::class => ['all' => true]
];

#[ORM\Table(name: 'cheque')]
#[AsEntityDTO]
class Cheque

$result = $this->entityManager
    ->getRepository(Cheque::class)
    ->baseQueryBuilder()
    ->select('cheque, city, shop, product')
    ->leftJoin('cheque.shop', 'shop')
    ->leftJoin('shop.city', 'city')
    ->leftJoin('cheque.orderList', 'orderList')
    ->leftJoin('orderList.product', 'product')
    ->setMaxResults(10)
    ->getQuery()
    ->getResult(Cheque::class);

array:2 [▼
  0 => App\Domain\Cheque\Entity\Cheque {#1107 ▼
    +price: "105.4"
    +chequeNumber: "0119-201703119-02-9380"
    +shop: App\Domain\Shop\Entity\Shop {#1091 ▶}
    +currency: ? App\Domain\Currency\Entity\Currency
    +orderList: ? Doctrine\Common\Collections\Collection
    +walletTransaction: ? App\Domain\Wallet\Entity\WalletTransaction
    #id: 4
    #date: DateTime @1489878000 {#1083 ▶}
    #createdAt: DateTime @1489878000 {#1081 ▶}
    #updatedAt: DateTime @1489878000 {#1071 ▶}
  }
  1 => App\Domain\Cheque\Entity\Cheque {#1094 ▼
    +price: "311.27"
    +chequeNumber: "0019-20170318-05-9278"
    +shop: App\Domain\Shop\Entity\Shop {#1141 ▶}
    +currency: ? App\Domain\Currency\Entity\Currency
    +orderList: ? Doctrine\Common\Collections\Collection
    +walletTransaction: ? App\Domain\Wallet\Entity\WalletTransaction
    #id: 5
    #date: DateTime @1489791600 {#1142 ▶}
    #createdAt: DateTime @1489791600 {#1139 ▶}
    #updatedAt: DateTime @1489791600 {#1138 ▶}
  }
]

array:2 [▼
  0 => ChequeRuntimeDTO {#1107 ▼
    +price: "105.4"
    +chequeNumber: "0119-201703119-02-9380"
    +shop: App\Domain\Shop\Entity\Shop {#1091 ▶}
    +currency: ? App\Domain\Currency\Entity\Currency
    +orderList: ? Doctrine\Common\Collections\Collection
    +walletTransaction: ? App\Domain\Wallet\Entity\WalletTransaction
    #id: 4
    #date: DateTime @1489878000 {#1083 ▶}
    #createdAt: DateTime @1489878000 {#1081 ▶}
    #updatedAt: DateTime @1489878000 {#1071 ▶}
  }
  1 => ChequeRuntimeDTO {#1094 ▼
    +price: "311.27"
    +chequeNumber: "0019-20170318-05-9278"
    +shop: App\Domain\Shop\Entity\Shop {#1141 ▶}
    +currency: ? App\Domain\Currency\Entity\Currency
    +orderList: ? Doctrine\Common\Collections\Collection
    +walletTransaction: ? App\Domain\Wallet\Entity\WalletTransaction
    #id: 5
    #date: DateTime @1489791600 {#1142 ▶}
    #createdAt: DateTime @1489791600 {#1139 ▶}
    #updatedAt: DateTime @1489791600 {#1138 ▶}
  }
]

 declare(strict_types=1);

namespace App\Domain\Cheque\EntityDTO;

use Danilovl\DoctrineEntityDtoBundle\Attribute\AsScalarDTO;

#[AsScalarDTO]
class ChequeDTO
{
    public function __construct(
        public readonly int $id,
        public readonly string $chequeNumber
    ) {}
}


ScalarHydration::$dtoClass = ChequeDTO::class;

$this->entityManager
    ->getRepository(Cheque::class)
    ->baseQueryBuilder()
    ->select('cheque.id, cheque.chequeNumber')
    ->setMaxResults(2)
    ->getQuery()
    ->getResult(ChequeDTO::class);

array:2 [▼
  0 => App\Domain\Cheque\EntityDTO\ChequeDTO {#1065 ▼
    +id: 4136
    +chequeNumber: "165791"
  }
  1 => App\Domain\Cheque\EntityDTO\ChequeDTO {#1066 ▼
    +id: 5838
    +chequeNumber: "539349913"
  }
]

$this->paginator->paginate($query->setHydrationMode(class::class));

$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, TranslationWalker::class);
$query->setHydrationMode(Product::class);
yaml
danilovl_doctrine_entity_dto:
  isEnableEntityDTO: true
  entityDTO:
    - App\Domain\Cheque\Entity\Cheque
yaml
danilovl_doctrine_entity_dto:
  isEnableScalarDTO: true
  scalarDTO:
    - App\Domain\Cheque\EntityDTO\ChequeDTO