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

BelongsTo return Integer instead of object Object

I have Entity:

class Booking extends Phalcon\Mvc\Model{

    public $Id;
    public $Route;

    public function initialize(){
        $this->belongsTo("Route","Route","Id");
    }
}

class Route extends Phalcon\Mvc\Model{

    public $DefaultData;
    public function initialize(){
        $this->belongsTo("DefaultData", "RouteDefaultData", "Id");
    }
}
class RouteDefaultData extends Phalcon\Mvc\Model{

    public $Id;
    public $RouteName;
}

When i try to do something like

//working
echo $booking->Route // this return integer

//not working
echo $booking->Route->DefaultData->RouteName;

To meniton one thing. I am using:

$Bookings = Booking::find(
        array(

            "columns"=> "Id, SlotTime, FlightDate, BookingStatus, Route",
            "conditions" => "FlightDate LIKE ?1",
            "bind"       => array(1 => date('Y').'-'.$selectedMonth."%")
        )
    );

Phalcon ORM Model returns object right? Not array.



98.9k

A field has the same name as a relation, so you can alias the relation name to avoid this conflict:

class Booking extends Phalcon\Mvc\Model{

    public $Id;
    public $Route;

    public function initialize(){
        $this->belongsTo("Route","Route","Id", array("alias" => "leRoute"));
    }
}
echo $booking->Route // this returns integer
echo $booking->leRoute->DefaultData->RouteName;


7.8k
edited Aug '14

Yes well that is not working:

Notice: Undefined property: Phalcon\Mvc\Model\Row::$leRoute

Any other Idea?



98.9k
Accepted
answer

Well, you have to remove the "columns" parameter in the findFirst/find or it won't work



7.8k

Yes now it is working. Doctrine has same problem. You should look into that maybe. I really do not need all columns. I need just three or one or whatever.

That sucks.