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.

Extra columns in ManyToMany Relation

Is there any way that I can add/access extra columns in many to many relation. These are my Entities (I will not copy all of theirs conetent):

class Customer extends Phalcon\Mvc\Model {

      public $Id; 
      public $FirstName; 
      public $LastName;
}

class Booking extends Phalcon\Mvc\Model{

        public $Id;
        public $FlightDate;
        public $CreatedAt;



        public function initialize(){
            $this->setSource("Booking");

            $this->hasManyToMany(
                "Id",
                "BookingCustomer",
                "Booking", "Customer",
                "Customer",
                "Id"
            );

            //define relations

            $this->CreatedAt = date("Y-m-d H:i:s");
        }
    }
    class BookingCustomer(){

          public $Booking;
          public $Customer;
          public $CustomerType // -> this is some text or integer;
          public function initialize(){

          $this->setSource("BookingCustomer");

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

          }
    }

Now when I do something like:

$booking = Booking::findFirst(854); ManyToMany relation is ok. And sql query that I see in log is ok. It just missing that third $CustomerType.

Am I doing this wrong way? I need be able to say this:

for booking id 854 get me customers which type is 0

I mean I can search manyToMany Entity and then once found to say ok I have Customer Id give me object customer with that id but that is kind of wrong way? Don't you think.

p.s. By the way I did not designed database and I can not change database.



4.5k
Accepted
answer
edited Aug '14

I think I solved this.

$booking = Booking::findFirst(854);
echo count($booking->getCustomer("CustomerType=0"));

Phlacon ROCKS!