I want to fetch all the contents and display them in one table from the tables below:
Petrolclaims table
$this->belongsTo('Workers', [
'foreignKey' => 'worker_id',
'joinType' => 'INNER'
//'through' => 'Selections'
]);
$this->belongsTo('Vehicles', [
'foreignKey' => 'vehicle_id',
'joinType' => 'INNER'
]);
Tollclaims table
$this->belongsTo('Workers', [
'foreignKey' => 'worker_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Vehicles', [
'foreignKey' => 'vehicle_id',
'joinType' => 'INNER'
]);
Workers table
<?php
namespace App\Model\Table;
use App\Model\Entity\Worker;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Workers Model
*
*/
class WorkersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->hasMany('Petrolclaims', [
'className' => 'Petrolclaims'
]);
$this->hasMany('Tollclaims', [
'className' => 'Tollclaims'
]);
$this->table('workers');
$this->displayField('name');
$this->primaryKey('id');
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
$validator
->add('createdDate', 'valid', ['rule' => 'datetime'])
->allowEmpty('createdDate', 'create');
return $validator;
}
}
Vehicles table
$this->hasMany('Petrolclaims', [
'className' => 'Petrolclaims'
]);
$this->hasMany('Tollclaims', [
'className' => 'Tollclaims'
]);
WorkersController
$results1 = $this->Workers->find('all')
->select([
'Petrolclaims.worker_id', 'Petrolclaims.vehicle_id'
'Tollclaims.worker_id', 'Tollclaims.vehicle_id',
'Workers.id',
'Vehicles.id'
])
->order(['Workers.name' => 'ASC'])
->contain([
'Tollclaims','Petrolclaims'
]);
How can I achieve this?