Skip to content

Bugfix/support db without json #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Storage/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Doctrine\JsonHelper;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Base;
use Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\ORM\QueryBuilder;
Expand Down Expand Up @@ -75,6 +75,9 @@ class SelectQuery implements QueryInterface
'author',
];

/** @var array */
protected $regularFields = [];

/** @var string */
protected $anything = 'anything';

Expand Down Expand Up @@ -263,6 +266,9 @@ public function build(): QueryBuilder

$dateFields = $this->getDateFields();

// Set the regular fields. They are needed for setting the correct param if DB does not support json.
$this->setRegularFields();

$whereExpression = $this->getWhereExpression();
if ($whereExpression) {
$query->andWhere($whereExpression);
Expand All @@ -275,6 +281,10 @@ public function build(): QueryBuilder
$param = date('c', strtotime($param));
}

if (in_array($fieldName, $this->regularFields, true)) {
$param = JsonHelper::wrapJsonFunction(null, $param, $query->getEntityManager()->getConnection());
}

$query->setParameter($key, $param, ParameterTypeInferer::inferType($param));
}

Expand Down Expand Up @@ -407,6 +417,11 @@ public function getTaxonomyFields(): array
return $this->taxonomyFields;
}

private function setRegularFields(): void
{
$this->regularFields = $this->getConfig()->get('contenttypes/' . $this->getContentType())->get('fields')->keys()->all();
}

private function getDateFields(): array
{
// Get all fields from the current contentType
Expand Down Expand Up @@ -481,7 +496,7 @@ private function getTaxonomyFieldExpression(Filter $filter): string
return sprintf('taxonomies_%s.slug = :%s', $filter->getKey(), key($filter->getParameters()));
}

private function getRegularFieldExpression(Filter $filter, EntityManager $em): string
private function getRegularFieldExpression(Filter $filter, EntityManagerInterface $em): string
{
$this->fieldJoins[$filter->getKey()] = $filter;
$expr = $this->qb->expr()->andX();
Expand Down