Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dql Bundle Laravel Package

baetmaen/dql-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require baetmaen/dql-bundle
    

    Register the bundle in config/bundles.php (Symfony) or AppServiceProvider (Laravel via Symfony bridge):

    Baetmaen\DqlBundle\BaetmaenDqlBundle::class => ['all' => true],
    
  2. First Use Case Extend Doctrine Query Language (DQL) with missing MySQL functions (e.g., DATE(), MONTH()). Example:

    // In a Doctrine QueryBuilder or DQL string
    $qb->expr()->func('DATE', 'entity.property');
    

    Or directly in DQL:

    SELECT DATE(entity.createdAt) FROM App\Entity\Post entity
    
  3. Where to Look First

    • Doctrine DQL Documentation: Understand how native functions work in DQL.
    • Bundle Configuration: Check config/packages/baetmaen_dql.yaml (if auto-generated) for custom function mappings.
    • Function List: Review the README (if updated) for supported functions (e.g., MONTH, YEAR, DAYOFWEEK).

Implementation Patterns

Workflows

  1. QueryBuilder Integration Use the expr()->func() method to inject custom functions:

    $qb = $entityManager->createQueryBuilder();
    $qb->select('DATE(p.createdAt) as postDate')
        ->from('App\Entity\Post', 'p')
        ->where($qb->expr()->func('MONTH', 'p.createdAt') . ' = :month')
        ->setParameter('month', 5);
    
  2. DQL String Usage Embed functions directly in DQL queries:

    $query = $entityManager->createQuery(
        'SELECT DAYOFWEEK(u.createdAt) as dayOfWeek FROM App\Entity\User u'
    );
    
  3. Custom Function Registration Extend the bundle to add unsupported functions (see Extension Points below).

  4. Performance Considerations

    • Prefer native SQL functions (e.g., DATE()) over PHP post-processing for large datasets.
    • Use INDEX hints if the bundle adds non-SARGable functions (e.g., MONTH() in WHERE clauses).

Integration Tips

  • Symfony Forms: Use the bundle with QueryBuilder in form types for filtered collections:
    $builder->add('posts', EntityType::class, [
        'class' => Post::class,
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('p')
                ->where('MONTH(p.createdAt) = MONTH(CURRENT_DATE())');
        },
    ]);
    
  • API Layer: Return formatted dates in API responses using the same functions:
    $serializer->serialize($post->getCreatedAt()->format('Y-m-d'), 'json', [
        'date_format' => 'Y-m-d',
    ]);
    
    (Note: Bundle functions are for DQL, not PHP strings—use PHP’s DateTime for output.)

Gotchas and Tips

Pitfalls

  1. Function Availability

    • The bundle only supports MySQL functions. Test on your DBMS (e.g., PostgreSQL may reject DAYOFWEEK).
    • No guarantee of completeness: The README suggests this is a "test" package. Verify supported functions via:
      // Check registered functions (if bundle exposes them)
      $this->get('baetmaen_dql.function_registry')->getFunctions();
      
  2. Doctrine Version Compatibility

    • Requires Symfony 2.3+ (via symfony/framework-bundle). For Laravel, use the Symfony Bridge.
    • Laravel-Specific: This bundle is not Laravel-native. Use Doctrine’s native DQL or Laravel Query Builder for simpler cases.
  3. Case Sensitivity

    • DQL functions are case-insensitive, but the bundle may enforce exact names (e.g., DATE() vs date()). Stick to uppercase for consistency.
  4. Parameter Binding

    • Avoid binding arguments to custom functions (e.g., DATE(:date)). Bind values after function application:
      // Wrong (may fail):
      $qb->where('DATE(:date) = :targetDate');
      // Correct:
      $qb->where('DATE(p.createdAt) = :targetDate')
          ->setParameter('targetDate', '2023-01-01');
      

Debugging

  1. Query Logging Enable Doctrine logging to verify function translation:

    $entityManager->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
    

    Expected output:

    SELECT DATE(`p`.`created_at`) AS date FROM `post` p WHERE MONTH(`p`.`created_at`) = 5
    
  2. Function Registration Errors If a function fails silently, check:

    • The function name is exactly as registered (e.g., MONTH, not month).
    • The bundle is loaded (verify config/bundles.php).

Extension Points

  1. Adding New Functions Override the function registry (if the bundle exposes it):

    # config/packages/baetmaen_dql.yaml
    baetmaen_dql:
        functions:
            - { name: 'QUARTER', class: 'Baetmaen\DqlBundle\DQL\Function\QuarterFunction' }
    

    Or create a custom registry service:

    // src/Service/CustomFunctionRegistry.php
    class CustomFunctionRegistry extends \Baetmaen\DqlBundle\DQL\Function\FunctionRegistry
    {
        public function __construct()
        {
            parent::__construct();
            $this->addFunction('QUARTER', new QuarterFunction());
        }
    }
    
  2. Custom Function Logic Extend Baetmaen\DqlBundle\DQL\Function\AbstractFunction to add logic:

    class QuarterFunction extends AbstractFunction
    {
        public function parse(\Doctrine\ORM\Query\Parser $parser)
        {
            $parser->match(Lexer::T_IDENTIFIER);
            $parser->match(Lexer::T_OPEN_PARENTHESIS);
            $arg = $parser->SimpleArithmeticExpression();
            $parser->match(Lexer::T_CLOSE_PARENTHESIS);
            return new Quarter($arg);
        }
    }
    

Configuration Quirks

  • No Default Config: The bundle may not generate a config file. Create config/packages/baetmaen_dql.yaml if needed.
  • Laravel-Specific: If using Laravel, ensure the bundle’s FunctionRegistry is bound to the container:
    // config/app.php
    'baetmaen_dql.function_registry' => Baetmaen\DqlBundle\DQL\Function\FunctionRegistry::class,
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui