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

Getting related models

Phalcon Version: 2.1 Beta 2.

I have a flight entity that belongs to an Airline, in my controller I have this code:

// get flight entity
$flight = Flights::findFirst([
    'conditions' => 'id = ?0 and status = ?1',
    'bind' => [$id, Flights::STATUS_OPEN]
]);

// echo flight airline title twice
dump($flight->Airline->title, $flight->Airline->title);

in this case I want echo flight airline title twice, on first call everything is fine, but on second call i got error:

 Catchable fatal error: Object of class Plugin\Demo\Model\Airlines could not be converted to string in C:\xampp\htdocs\Project\app\Plugin\Demo\Model\Airlines.php on line 115

Flight Model Relations

 public function initialize()
    {
        $this->hasMany('id', 'Plugin\Demo\Model\FlightsEvents', 'flight', ['alias' => 'Events']);
        $this->hasMany('id', 'Plugin\Demo\Model\FlightsPassengers', 'flight', ['alias' => 'Passengers']);
        $this->belongsTo('aircraft', 'Plugin\Demo\Model\Aircrafts', 'id', ['alias' => 'Aircraft']);
        $this->belongsTo('airline', 'Plugin\Demo\Model\Airlines', 'id', ['alias' => 'Airline']);
        $this->belongsTo('airport', 'Plugin\Demo\Model\Airports', 'id', ['alias' => 'Airport']);
    }

what is problem?



17.5k

$flight->getAirline()->title;