Many to Many

I have my Model Job that has a relation to my model Location.

The Job table has 2 columns pickup and delivery and those 2 columns has a foreign key to my location table.

Now when I print the data of the Job model I get my array of pickup and delivery with their ID numbers but what I need is to get the name of the location and delivery which is in the location table.

I already tried this:




public function getJob()

    {

        return $this->hasOne(Job::className(), ['id' => 'job_id']);

    }


public function getLocationName()

    {

    	return $this->hasMany(Location::className(), ['location_id' => 'pickup_location'])

    	->via('job');

    }



but that return only the name of the pickup_location but I also need the name of the delivery_location at the same time.

Are im right saying you have something like this?




Job

    ID

    delivery_id (=> Location)

    pickup_id (=> Location)

    ...

        

Location

    id

    ...

        

public function getDeliveryLocation() {

    return $this->hasMany(Location::className(), ['location_id' => 'delivery_id'])

}


public function getPickupLocation() {

    return $this->hasMany(Location::className(), ['location_id' => 'pickup_id'])

}



Or does i understand you wrong?

check if it work for you : http://stackoverflow.com/questions/26763298/how-do-i-work-with-many-to-many-relations-in-yii2