2017 © Pedro PelĂĄez
 

library doctrine-ransack

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

image

paliari/doctrine-ransack

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  • Monday, December 4, 2017
  • by paliari
  • Repository
  • 1 Watchers
  • 2 Stars
  • 51 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

doctrine-ransack

Installation

$ composer require paliari/doctrine-ransack

Configuration

Your setup, example, (*1)

<?php
use Paliari\Doctrine\Ransack;
use Paliari\Doctrine\RansackConfig;

$ransack = new Ransack(new RansackConfig($entityManager));

Usage

<?php
use Paliari\Doctrine\Ransack;
use Paliari\Doctrine\RansackConfig;
use Paliari\Doctrine\VO\RansackOrderByVO;
use Paliari\Doctrine\VO\RansackParamsVO;

$entityName = User::class;
$alias = 't';
$paramsVO = new RansackParamsVO();
$paramsVO->where = [
    'person.address.street_cont' => 'Av% Brasil',
    'or' => [
        'name_eq' => 'Jhon',
        'email_start' => 'jhon',
        'person.address.city_eq' => 'MaringĂĄ',
    ],
    'id_order_by' => 'asc',
];
$paramsVO->orderBy = [
    new RansackOrderByVO(['field' => 'person.name', 'order' => 'ASC']),
    new RansackOrderByVO(['field' => 'person.id', 'order' => 'DESC']),
];
$paramsVO->groupBy = [
    'person.name',
    'person.address_id',
];
$qb = $entityManager->createQueryBuilder()->from($entityName, $alias);
$ransackBuilder = $this->ransack
    ->query($qb, $entityName, $alias)
    ->includes()
    ->where($paramsVO);
$users = $ransackBuilder->getQuery()->getResult();

// Using includes
$includes = [
  'only' => ['id', 'email'],
  'include' => [
    'person' ['only' => ['id', 'name']],
  ],
];
$rows = $ransackBuilder->includes($includes)->getArrayResult();


Custom Association

Your class of get custom association, (*2)

<?php

use Doctrine\ORM\Query\Expr\Join;
use Paliari\Doctrine\CustomAssociationInterface;
use Paliari\Doctrine\VO\RelationVO;
use Paliari\Doctrine\VO\JoinVO;
use Person;
use User;

class CustomAssociation implements CustomAssociationInterface
{
    public function __invoke(string $entityName, string $alias, string $field): ?RelationVO
    {
        if (User::class === $entityName && 'custom' == $field) {
            $relationVO = new RelationVO();
            $relationVO->entityName = $entityName;
            $relationVO->fieldName = $field;
            $relationVO->targetEntity = Person::class;
            $joinVO = new JoinVO();
            $joinVO->join = Person::class;
            $joinVO->alias = "{$alias}_$field";
            $joinVO->conditionType = Join::WITH;
            $joinVO->condition = "$alias.email = $joinVO->alias.email";
            $relationVO->join = $joinVO;

            return $relationVO;
        }

        return null;
    }
}

Setup with CustomAssociation, (*3)

<?php
use Paliari\Doctrine\Ransack;
use Paliari\Doctrine\RansackConfig;

$customAssociation = new CustomAssociation();
$config = new RansackConfig($entityManager, $customAssociation);
$ransack = new Ransack($config);

$entityName = User::class;
$alias = 't';
$paramsVO = new RansackParamsVO();
$paramsVO->where = [
    'custom.email_eq' => 'your-email@gmail.com',
];
$includes = [
  'only' => ['id', 'email'],
  'include' => [
    'custom' ['only' => ['id', 'name']],
  ],
];
$qb = $entityManager->createQueryBuilder()->from($entityName, $alias);
$ransackBuilder = $this->ransack
    ->query($qb, $entityName, $alias)
    ->includes()
    ->where($paramsVO);
$users = $ransackBuilder->getQuery()->getResult();

Filters

The filters must be passed in a hash with the name of the key containing the field ending with the predicates below ex: person.name_eq, person.id_gt., (*4)

It is also possible to combine predicates within or or and clauses, eg:, (*5)

$where = [
    'name_cont' => 'Jhon',
    'or' => [
        'person.name_start' => 'Jhon',
        'person.email_end' => '@gmail.com',
        'and' => [
            'person.address.city_eq' => 'MaringĂĄ',
            'person.address.state_eq' => 'PR',
        ],
    ],
];

List of all possible predicates

  • *_eq (equal)

    • Example:, (*6)

      {"col_eq": "Fulano da Silva"}
      
    • SQL result:, (*7)

      WHERE col = 'Fulano da Silva'
      
  • *_not_eq (not equal)

    • Example:, (*8)

      {"col_not_eq": "Fulano da Silva"}
      
    • SQL result:, (*9)

      WHERE col <> 'Fulano da Silva'
      
  • *_in (match any values in array)

    • Example:, (*10)

      {"col_in": [13, 21, 124, 525]}
      
    • SQL result:, (*11)

      WHERE col IN (13, 21, 124, 525)
      
  • *_not_in (match none of values in array)

    • Example:, (*12)

      {"col_not_in": [13, 21, 124, 525]}
      
    • SQL result:, (*13)

      WHERE col NOT IN (13, 21, 124, 525)
      
  • *_null (is null)

    • Example:, (*14)

      {"col_null": null}
      
    • SQL result:, (*15)

      WHERE col IS NULL
      
  • *_not_null (is not null)

    • Example:, (*16)

      {"col_not_null": null}
      
    • SQL result:, (*17)

      WHERE col IS NOT NULL
      
  • *_present (not null and not empty)

    Only compatible with string columns., (*18)

    • Example:, (*19)

      {"col_present": 1}
      
    • SQL result:, (*20)

      WHERE col IS NOT NULL AND col != ''
      
  • *_blank (is null or empty)

    Only compatible with string columns., (*21)

    • Example:, (*22)

      {"col_blank": 1}
      
    • SQL result:, (*23)

      WHERE col IS NULL OR col = ''
      
  • *_lt (less than)

    • Example:, (*24)

      {"col_lt": 25}
      
    • SQL result:, (*25)

      WHERE col < 25
      
  • *_lteq (less than or equal to)

    • Example:, (*26)

      {"col_lteq": 25}
      
    • SQL result:, (*27)

      WHERE col <= 25
      
  • *_gt (greater than)

    • Example:, (*28)

      {"col_gt": 25}
      
    • SQL result:, (*29)

      WHERE col > 25
      
  • *_gteq (greater than or equal to)

    • Example:, (*30)

      {"col_gteq": 25}
      
    • SQL result:, (*31)

      WHERE col >= 25
      
  • *_matches (matches with LIKE)

    • Example:, (*32)

      {"col_matches": "Fulano"}
      
    • SQL result:, (*33)

      WHERE col LIKE 'Fulano'
      
  • *_not_matches (does not match with LIKE)

    • Example:, (*34)

      {"col_not_matches": "Fulano"}
      
    • SQL result:, (*35)

      WHERE col NOT LIKE 'Fulano'
      
  • *_cont (contains value)

    • Example:, (*36)

      {"col_cont": "Fulano"}
      
    • SQL result:, (*37)

      WHERE col LIKE '%Fulano%'
      
  • *_not_cont (does not contain)

    • Example:, (*38)

      {"col_not_cont": "Fulano Silva"}
      
    • SQL result:, (*39)

      WHERE col NOT LIKE '%Fulano%Silva%'
      
  • *_start (starts with)

    • Example:, (*40)

      {"col_start": "Fulano"}
      
    • SQL result:, (*41)

      WHERE col LIKE 'Fulano%'
      
  • *_not_start (does not start with)

    • Example:, (*42)

      {"col_not_start": "Fulano"}
      
    • SQL result:, (*43)

      WHERE col NOT LIKE 'Fulano%'
      
  • *_end (ends with)

    • Example:, (*44)

      {"col_end": "Fulano"}
      
    • SQL result:, (*45)

      WHERE col LIKE '%Fulano'
      
  • *_not_end (does not end with)

    • Example:, (*46)

      {"col_not_end": "Fulano"}
      
    • SQL result:, (*47)

      WHERE col NOT LIKE '%Fulano'
      
  • *_between (between in 2 values)

    • Example:, (*48)

      {"col_between": [10, 20]}
      
    • SQL result:, (*49)

      WHERE col BETWEEN 10 AND 20
      

Authors

The Versions

04/12 2017

dev-master

9999999-dev https://github.com/paliari/doctrine-validator

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/12 2017

1.1.3

1.1.3.0 https://github.com/paliari/doctrine-validator

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/12 2017

1.1.2

1.1.2.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/12 2017

1.1.1

1.1.1.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/10 2016

1.1.0

1.1.0.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

16/09 2016

1.0.5

1.0.5.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

16/09 2016

1.0.4

1.0.4.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

16/09 2016

1.0.3

1.0.3.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

15/09 2016

1.0.2

1.0.2.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

15/09 2016

1.0.1

1.0.1.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires

15/09 2016

1.0.0

1.0.0.0

Advanced query searching for Doctrine ORM (based on Ruby on Rails Ransack)

  Sources   Download

MIT

The Requires

 

The Development Requires