orm: Matching criteria on the InverseSide of a ManyToMany relation fails
I’m using DBAL and ORM 2.5.4, with PHP 5.5.15.
I have two classes (Asset and Process) which are linked by a ManyToMany relation as follows:
class Asset extends Base {
/**
* @Doctrine\ORM\Mapping\Id
* @Doctrine\ORM\Mapping\Column(type="integer", options={"unsigned"=true})
* @Doctrine\ORM\Mapping\GeneratedValue
**/
protected $id;
/**
* @Doctrine\ORM\Mapping\ManyToMany(targetEntity="Process", mappedBy="assets")
**/
protected $processes;
}
class Process extends Base {
/**
* @Doctrine\ORM\Mapping\Id
* @Doctrine\ORM\Mapping\Column(type="integer", options={"unsigned"=true})
* @Doctrine\ORM\Mapping\GeneratedValue
**/
protected $id;
/**
* @Doctrine\ORM\Mapping\ManyToMany(targetEntity="Asset", inversedBy="processes")
* @Doctrine\ORM\Mapping\OrderBy({"id" = "ASC"})
**/
protected $assets;
/**
* @Doctrine\ORM\Mapping\Column(type="string")
**/
protected $status;
Now I’m trying to filter Active’s Processes by a field (status) as follows:
$expr = \Doctrine\Common\Collections\Criteria::expr();
$criteria = \Doctrine\Common\Collections\Criteria::create()
->where($expr->eq("status", 'finished'));
$this->getProcesses()->matching($criteria);
If I understand correctly, this should work, but instead I’m getting a bunch of exceptions:
Notice: Undefined index: relationToSourceKeyColumns in vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Collection\ManyToManyPersister.php on line 234
And
Notice: Undefined index: name in vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\DefaultQuoteStrategy.php on line105
I’ve walked through the code and the problem seems to be that on ManyToManyPersister::loadCriteria()
$joinTable is not loading correctly (it’s returning an empty string):
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
Which creates this SQL query:
SELECT te.id AS id, te.status AS status FROM processes te JOIN t ON t.process_id = te.id WHERE te.status = ?' with params ["finished"]
Note how the JOIN table is missing from the query (JOIN t
).
Setting JoinTable excplicitly on the Owning side (Process) does not make any difference, but setting it on the Inverse side (Asset) makes the SQL query be built correctly, but those 2 errors still happen:
Notice: Undefined index: relationToSourceKeyColumns in vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Collection\ManyToManyPersister.php on line 234
And
Notice: Undefined index: name in vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\DefaultQuoteStrategy.php on line105
Now, if I understand correctly, setting the JoinTable should not be necessary, and everything should work with no errors… Is this a bug, or did I misunderstand something?
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 16
- Comments: 15 (2 by maintainers)
I’m having exactly the same problem. There are other bugs in the Criteria functionality too (#5687 for example). The more I use it the more I think it’s not very well tested or ready for release.
More than a year has passed but it’s still not fixed, weird
Me too. Can someone give an example of using a repository instead of Criteria for the exact same purpose? Thanks.
It is much appreciated if a PR with a failing test is contributed. PRs with a fix is also welcome.
I would love to contribute however I’m totally out of time, so are the authors I guess. No knowledge about Doctrine’s internals does not help as well. There are already 162 pull requests pending! lol. It would be nice if any organization thrown some money to the contributors to help maintain this project. Beside Composer I find Doctrine to be one of the most important libraries out there driving the PHP world.
@AlexeyKosov it’s still not clear that this is a bug. It’s just that the maintainers don’t have enough time to investigate every single of them. If you want to make it, create a reproducible scenario and submit a PR.