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.

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.



83.4k

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;


4.5k
edited Aug '14

Yes well that is not working:

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

Any other Idea?



83.4k
Accepted
answer

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



4.5k

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.