PHP code example of dbstudios / doctrine-query-document
1. Go to this page and download the library: Download dbstudios/doctrine-query-document 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/ */
dbstudios / doctrine-query-document example snippets
// $objectManager should be an instance of Doctrine\Common\Persistence\ObjectManager
$manager = new QueryManager($objectManager);
$queryBuilder = $objectManager->createQueryBuilder()
->from('App\Entity\MyEntity', 'e')
->select('e');
$manager->apply($queryBuilder, [
'field' => 'value',
'otherField' => [
'$gt' => 10,
],
]);
echo $queryBuilder->getDQL();
// SELECT e FROM App\Entity\MyEntity e WHERE field = ?0 AND otherField > ?1
// $objectManager should be an instance of Doctrine\Common\Persistence\ObjectManager
$manager = new QueryManager($objectManager);
$queryBuilder = $objectManager->createQueryBuilder()
->from('App\Entity\MyEntity', 'e')
->select('e');
$manager->apply($queryBuilder, [
'otherEntity.field' => 'value',
]);
echo $queryBuilder->getDQL();
// SELECT e FROM App\Entity\MyEntity e JOIN e.otherEntity join_1 WHERE join_1.field = ?0
// $objectManager should be an instance of Doctrine\Common\Persistence\ObjectManager
$manager = new QueryManager($objectManager);
$queryBuilder = $objectManager->createQueryBuilder()
->from('App\Entity\MyEntity', 'e')
->select('e');
$manager->apply($queryBuilder, [
'attributes.myField' => 'value',
]);
echo $queryBuilder->getDQL();
// SELECT e FROM App\Entity\MyEntity e WHERE JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.myField')) = ?0
// $objectManager should be an instance of Doctrine\Common\Persistence\ObjectManager
$manager = new QueryManager($objectManager);
$queryBuilder = $objectManager->createQueryBuilder()
->from('App\Entity\MyEntity', 'e')
->select('e');
$manager->apply($queryBuilder, [
'otherEntityName' => 'value',
]);
$projection = new Projection([
'id' => true,
'name' => false,
], true);
// Both are the "explicit" variants of their respective behavior, since the keys are present in the list.
assert($projection->isAllowedExplicitly('id'));
assert($projection->isDeniedExplicitly('name'));
// The Projection is configured to allow by default (the second constructor argument), so fields not present in the
// list are allowed.
assert($projection->isAllowed('foo'));
// However, the default allow behavior is not an explicit allow, as the key is _not_ in the list.
assert(!$projection->isAllowedExplicitly('foo'));
$projection = new Projection([
'foo' => [
'*' => false,
'bar' => true,
]
]);
echo QueryResult::describe($projection->query('foo.bar')); // "explicit allow"
echo QueryResult::describe($projection->query('foo.baz')); // "explicit deny"
// Makes sense so far. But what about querying the parent node?
echo QueryResult::describe($projection->query('foo')); // "explicit allow"
use DaybreakStudios\DoctrineQueryDocument\OperatorInterface;
use DaybreakStudios\DoctrineQueryDocument\QueryDocumentInterface;
use Doctrine\ORM\Query\Expr\Composite;
class EqualsOperator implements OperatorInterface {
/**
* {@inheritdoc}
*/
public function getKey(): string {
return 'eq';
}
/**
* {@inheritdoc}
*/
public function process(QueryDocumentInterface $document, string $key, $value, Composite $parent): void {
$document->expr()->eq($parent, $key, $value);
}
}
// $objectManager should be an instance of Doctrine\Common\Persistence\ObjectManager
$manager = new QueryManager($objectManager);
$manager->setOperator(new EqualsOperator());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.