They key is to set up the relations on every model:
class Product extends PhModel
{
public function initialize() {
$this->hasMany('id','ProductImage','product_id',['alias'=>'product_images']);
$this->hasManyToMany('id','ProductImage','product_id','image_id','Image','id',['alias'=>'images']);
}
}
class Image extends PhModel
{
public function initialize() {
$this->hasMany('id','ProductImage','image_id',['alias'=>'product_images']);
$this->hasManyToMany('id','ProductImage','image_id','product_id','Product','id',['alias'=>'products']);
}
}
// This isnt really necessary
class ProductImage extends PhModel
{
public function initialize() {
$this->belongsTo('product_id','Product','id',['alias'=>'products']);
$this->belongsTo('image_id','Image','id',['alias'=>'images']);
}
}
Now you can access the intermediate model:
$product = Product::findFirst();
foreach($product->product_images as $i) {
var_dump($i->toArray());
// $i->is_main
}
foreach($product->images as $i) {
var_dump($i->toArray());
// $i->path
}
https://docs.phalcon.io/en/latest/reference/models.html#defining-relationships