We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Many to Many Relation query

Hey guys! I have a Many-to-many relation that works as I want to, everything fine. My Question now: I want do a query like

SELECT * FROM robot INNER JOIN robot_mm_parts ON ( robot.uid = robot_mm_parts.uid_local AND robot_mm_parts.uid_foreign = 4);

robot_mm_parts.uid_foreign is the uid of the RobotParts table. So in other word: I want all robots with a certain robot part, how can I realize this with "Robots->find()"?

Thanks for helping me!



98.9k
Accepted
answer

This way:

$robots = $this->modelsManager->createBuilder()
    ->from('Robots')
    ->join('RobotsParts', 'Robots.uid = RobotsParts.uid_local AND RobotsParts.uid_foreign = 4')
    ->getQuery()
    ->execute();

I think I have to join the MM Table right? ;)



98.9k

I can't understand your question

Sorry for my late response, I got it to work with your example, I just added my relation table "robotmmparts" to that query and it works Thx!