Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Get specific children records


Let's say I have the following two models:

class Country extends Model {
    public $id;
    public $name;

    public function initialize() {
        $this->hasMany("id", "City", "country_id", [
            'alias' => 'Cities'
class City extends Model {
    public $id;
    public $country_id;
    public $name;
    public $isCapital;

    public function initialize() {
        $this->belongsTo("country_id", "Country", "id", [
            'alias' => 'Country'

And an Iceland object:

$Iceland = Country::findFirstByName('iceland');

Is there a way to get the capital of Iceland from the $Iceland object ?

I know of the filter() method, which involves a user defined function, but I'd like to know if there's a more "compact" way. Something like:

// I know it's invalid
$IcelandCapital = $Iceland->Cities::findFirst(['isCapital' => 1]);

Thanks in advance.


The following should work:

echo $Iceland->getCities('isCapital = 1')->name;

edited Nov '14

OK, looks like it should be:

echo $Iceland->getCities('isCapital = 1')[0]->name;

but I get the idea. Thanks Phalcon, this is what I was looking for!