Skip to content
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

exclude post list from the default of saved search query #5026

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static function fromRequest(int $id, Request $request): self
throw new \InvalidArgumentException('Id must be a positive number');
}
$query = new self($id);
$query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS);
$excluded_relations = ['posts'];
$query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS, $excluded_relations);
return $query;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static function fromRequest(Request $request): self
$query = new self();
$query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT);
$query->setSearchFields(new SavedSearchSearchFields($request));
$query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS);
$excluded_relations = ['posts'];
$query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS, $excluded_relations);
return $query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait QueryWithOnlyParameter
{
/**
/**
* @var array
*/
private $fields;
Expand Down Expand Up @@ -46,10 +46,14 @@ public function getFieldsForRelationship(): array
* @param Request $request
* @param array $approved_fields
*/
private function getOnlyValuesFromRequest(Request $request, array $allowed_fields, array $allowed_relations): void
{
private function getOnlyValuesFromRequest(
Request $request,
array $allowed_fields,
array $allowed_relations,
array $excluded_relations = [] // exclude them if they are not in only in request
): void {
$this->fields = $allowed_fields;
$this->hydrates = array_keys($allowed_relations);
$this->hydrates = array_diff(array_keys($allowed_relations), $excluded_relations);

if ($request->query('format') === 'minimal') {
$this->fields = ['id', 'name', 'description', 'translations'];
Expand Down Expand Up @@ -88,15 +92,24 @@ private function getNeededRelations(array $allowed_relations)
}
}
}
public function addOnlyParameteresFromRequest(Request $request, array $allowed_fields, array $allowed_relations, array $required_fields): void
{
$this->getOnlyValuesFromRequest($request, $allowed_fields, $allowed_relations);
public function addOnlyParameteresFromRequest(
Request $request,
array $allowed_fields,
array $allowed_relations,
array $required_fields,
array $relations_to_prune = [] // exclude them if they are not in request
): void {
$this->getOnlyValuesFromRequest($request, $allowed_fields, $allowed_relations, $relations_to_prune);
$this->fields = array_unique(array_merge($this->fields, $required_fields));
$this->getNeededRelations($allowed_relations);
}

public function addOnlyValues(array $fields, array $hydrates, array $allowed_relations, array $required_fields)
{

public function addOnlyValues(
array $fields,
array $hydrates,
array $allowed_relations,
array $required_fields
) {
$this->fields = $fields;
$this->hydrates = $hydrates;
$this->fields = array_unique(array_merge($this->fields, $required_fields));
Expand Down
Loading