php - PhalconPHP - get specific object with multiple relations


I have a model, call it Robot, which has multiple manyToMany relationships with other models - Part, Country and Permision. Relation models being RobotsParts, RobotsCountries and RobotsPermissions.

Each robot can have multiple or no parts, countries and permissions linked to them.

To get all the robots with a certain part, PhalconPHP makes it easy. (aliases being properly set in models, of course).

$part = Part::findFirstByName("arm");
$robotsWithPart = $part->robots;

The same thing applies for robots with a certain country:

$country = Country::findFirstByCode("HR");
$robotsWithCountry = $country->robots;

But how can one get only robots with a certain part, country and permission?

I've had futile attempts like:

$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");

$robots = $country->getRobots([
    'conditions' => "(partId = :pid:)",
    'bind' => [
        'pid' => $part->id

But, of course, partId is not recognized as it doesn't belong to any of the selected models;




You can use the$model->getRelated('model', $parameters = []) option.
$parameters = [] works the same as how you would normally query a model. i.e; it takes the parametersorder,limit,conditions, ...

$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");

$robots = $country->getRelated('Robot', ['partId = ' . $part->id]);

You can find this in the documentation


That sounds like it wouldn't be possible. You will have to call a custom query on yourRobot model. Something like this:

$result = Robot::query()
        ->join('Country', ' = Robot.countryId')
        ->join('Part', 'Part.robotId =')
        ->where('Country.code = :code:')
        ->where(' = :name:')
        ->bind(['code' => 'HR', 'name' => 'arm'])

You can also use the Querybuilder, if you prefer to use that.

People are also looking for solutions to the problem: javascript - UPDATE database after approval from admin


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.