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

Problems with ManyToMany Relation in namespaces

Booking model:

namespace TLH\Models;
class Booking extends \Phalcon\Mvc\Model{
    public function initialize(){
        $this->setSource('Booking');

        //define relations
        $this->hasManyToMany(
            "Id",
            "TLH\Models\BookingCustomer",
            "Booking", "Customer",
            "TLH\Models\Customer",
            "Id"
        );
        $this->belongsTo("Route","TLH\Models\Route","Id", array("alias" => "RouteInfo"));
    }
 }

ManyToMany Entity

 namespace TLH\Models;
 class BookingCustomer extends \Phalcon\Mvc\Model{

    public $Booking;
    public $Customer;
    public $CustomerType;

    public function initialize(){

        $this->setSource("BookingCustomer");

        //define relations column, entity, id
        $this->belongsTo("Booking", "TLH\Models\Booking", "Id");
        $this->belongsTo("Customer", "TLH\Models\Customer", "Id");

    }
 }

I am geting this error:

 The method "CustomerInfo" doesn't exist on model "TLH\Models\Booking"

 //for
 $Booking->CustomerInfo("CustomerType=0")

 //or
 The method "getCustomerInfo" doesn't exist on model "TLH\Models\Booking"

 //for
 $Booking->getCustomerInfo("CustomerType=0")


7.8k

Solved by adding alias:

array('alias' => 'CustomerInfo') 
//to many to many realtion

Phalcon rocks.