Answers for "doctrine orx"

PHP
0

doctrine orx

/**
 * orX stands for OR Expressions. They may contain multiple standard expressions
 * such as eq, neq, instanceof, etc. OrX can be used in two ways:
 * 1. Via Doctrine's Criteria library inside of an entity/class
 * 2. Via the QueryBuilder library inside of a query
 */

// Inside of a class
use Doctrine\Common\Collections\Criteria;
class Company
{
  /**
   * @ORM\OneToMany(targetEntity="Employee", mappedBy="company")
   */
  protected employees;

  function findDevelopers(): Collection
  {
	$expr     = Criteria::expr();
	$criteria = Criteria::create();

    $criteria->where($expr->eq('position', 'developer'));

    return $this->employees->matching($criteria);
  }
}
                        
// Inside of a query via querybuilder
function getStuff(): Array
{
  $qb = $entityManager->createQueryBuilder();
  
  $qb->select('assoc1.thing as thing1, assoc2.thing as thing2')
      ->from(assoc1::class, 'assoc1')
      ->innerJoin(assoc2::class, 'assoc2', 'with', 'assoc1.someId = assoc2.someId')
      ->where(
        $qb->expr()->orX(
          $qb->expr()->eq('assoc1.thing', 'foo'),
          $qb->expr()->neq('assoc2.thing', 'bar')
        )
      )
      ->getQuery()
      ->getResult();
}
Posted by: Guest on August-25-2021

Browse Popular Code Answers by Language