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

How to get fields from Relational Table in Phalcon Find Query

I have two tables 'Listings' and 'User'. In Listings Table i have user_id which belongs to Users table. I want 'email' field value from Users table when I find a Listing.

In Listing Model , I included phalcon belogs to code in initialize() function.

$this->belongsTo('user_id', 'Ezydir\Models\Users', 'id', array( 'alias' => 'User' ));

And In Controller :

$parameters["columns"] = array("id","title","status","manage_level_id","paid_status","User.email");

    $listings = Listings::find($parameters);

But Its Not Working if I check in View : $listings->User->id;

Model::find() returns a Result set, not a model itself. If you wanted to grab the relationship you have to iterate through the resultset

$listings = Listings::find($parameters);

foreach($listings as $list){
    $list->User->id; //Do something
}

Ya,I Know.. I used Paginator,

In Controller :

$listings = Listings::find($parameters);

    $paginator = new Paginator(array(
        "data" => $listings,
        "limit"=> $limit,
        "page" => $numberPage
    ));

    $this->view->page = $paginator->getPaginate();

In View : <?php foreach ($page->items as $listings) { $id = $listings->id; $email = $listings->User->email; }?> Its not working...
My Question is, in $parameters["columns"] how to add relational table field name ?
$parameters["columns"] = array("id","title","status","managelevelid","paid_status","User.email");

Try

    $listings = Listings::find();
    $listings->User;