Contain with alias not working in cakephp 5.x

26 views Asked by At

I've set an alias 'Vehicle' for the 'vehicle' table. However, when retrieving the results, the object name is 'vehicle' (the table name) instead of the expected alias 'Vehicle'.

The output is

screenshot

Below is the code of my model class Vehicle

namespace App\Model\Table;

use Cake\ORM\Table;

class VehicleTable extends Table
{
    public function initialize(array $config): void
    {
        parent::initialize($config);
        $this->setTable( 'vehicle' );
         $this->setAlias('Vehicle');
        $this->addBehavior('Timestamp');

      
   }

    }

In this class I am joining vehicle table and I am calling this getDetails function inside the controller

namespace App\Model\Table;

use App\Lib\Utility;

use Cake\Collection\Collection;
use Cake\ORM\Table;

class TripTable extends Table {
    public function initialize( array $config ): void {
        parent::initialize( $config );
        $this->setTable( 'trip' );
        $this->addBehavior( 'Timestamp' );
        
         $this->belongsTo('Vehicle', [
            'foreignKey' => 'vehicle_id',
        ] );
}



    public function getDetails($driver_id,$starting_point)
    {

        $query = $this->find()
                      ->where( [ 'Trip.user_id' => $driver_id ] )
                      ->where( [ 'Trip.completed' => 0 ] )
                      ->contain( [
                        
                          'Vehicle'
                          
                        
                      ] )
                      ->limit( 10 )
                      ->offset( $starting_point * 10 )
                      ->orderby( [ 'Trip.id' => 'DESC' ] );

        $result = $query->toArray();
        return $result;

    }


}
0

There are 0 answers