[3.x]: Extend ElementQuery and being unable to modify the list of sites using siteId()
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
What happened?
Description
I've created a Widget that makes a query and wanted to filter by site, but I always get the default site in the query instead
$submissions = Submission::find()
->siteId($this->sites) // this does nothing
->siteIdOverride($this->sites) // this sets the siteId correct site list
->all();
class SubmissionQuery extends ElementQuery
{
/**
* Can't use siteId directly, since it will be overwritten when the prepare method is called
*
* @param array $value
* @return $this
*/
public function siteIdOverride(array $value): self
{
$this->sitesOverride = $value;
return $this;
}
protected function beforePrepare(): bool
{
$sites = $this->siteId; // has default site in it
$sitesOverride = $this->sitesOverride; // correct list of sites
$this->siteId($this->sitesOverride);
return parent::beforePrepare();
}
}
Expected behavior
If a site is already set, craft should perform the query with that siteId instead
Actual behavior
$siteId will be overwriten with the default site in the prepare statement
Craft CMS version
3.7.26
PHP version
8.1
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the SubmissionQuery example and trace ElementQuery's beforePrepare handling of siteId, including when the default site is applied. Reproduce the query using an explicitly supplied site list and confirm that the generated query uses that list instead of overwriting it with the default site.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100