PHP code example of wertelko / doctrine-easy-join

1. Go to this page and download the library: Download wertelko/doctrine-easy-join 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/ */

    

wertelko / doctrine-easy-join example snippets


class CategoryRepository extends ServiceEntityRepository
{
    use JoinTrait; // <-- add trait

    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Category::class);
    }
    
    // Other methods
}

class CategoryController extends AbstractController
{
    public function __construct(
        private readonly CategoryRepository $categoryRepository,
    ) {
    }

    public function index(): Response
    {
        $category = $this->categoryRepository
            ->join([ // load eager with products and products images
            'products', // <-- $category->getProducts() is initialized
            'products.image' // <-- $category->getProducts()->first()->getImage() is initialized too
            ])
            ->getQuery()
            ->getResult();
        
        return $this->json($category);
    }
}

//code
$this->categoryRepository
    ->join(
        $fields, // Array of fields
        $joinType, // String of join type ['left', 'inner'] 
        $qb, // QueryBuilder
    )
//code