如何在 cakephp 3.x 的控制器中使用绑定多个表?

How to use bind with many tables in controller in cakephp 3.x?

我有三个table

在活动中 table

id
event_name
patient_id
madical_case_id
fees

在 madicalCases table

id
patient_id
case_type
date

患者table

id
name
address

现在我尝试使用 bind(madicalCases and Patients) 查找所有事件。 但没有成功


**i try this**

$this->belongsTo('Patients', ['foreignKey' => 'patient_id', 'joinType' => 'INNER']);
$this->belongsTo('MadicalCases', ['foreignKey' => 'madical_case_id', 'joinType' => 'INNER']);
$event = $this->Events->find()->where(['patient_id' => $patient_id], ['madical_case_id' => $mc_id])->all();

            OR

$this->belongsTo('MadicalCases', ['foreignKey' => 'madical_case_id', 'joinType' => 'INNER','Patients', ['foreignKey' => 'patient_id', 'joinType' => 'INNER']]);
$event = $this->Events->find()->where(['patient_id' => $patient_id], ['madical_case_id' => $mc_id])->all();     

这里有些错误

Call to undefined method App\Controller\EventsController::belongsTo() in E:\wamp\www\Gurukrupa\src\Controller\EventsController.php on line 36

Unable to emit headers. Headers sent in file=E:\wamp\www\Gurukrupa\src\Controller\EventsController.php line=36 [CORE\src\Http\ResponseEmitter.php, line 48]

Cannot modify header information - headers already sent by (output started at E:\wamp\www\Gurukrupa\src\Controller\EventsController.php:36) [CORE\src\Http\ResponseEmitter.php, line 149]

Cannot modify header information - headers already sent by (output started at E:\wamp\www\Gurukrupa\src\Controller\EventsController.php:36) [CORE\src\Http\ResponseEmitter.php, line 181]

哦,伙计,你试图破坏 MVC 框架的良好设计美感。您不能在 CakePHP 版本 3 的控制器中绑定模型或取消绑定模型。它们在 V3 中不再支持。它必须通过 Table 对象来完成。现在,您尝试在控制器中调用 belongTo。检查详细信息 here 可以这样做。

class MadicalCasesTable extends Table{
    public function initialize(array $config){
    $this->belongsTo('Patients')
        ->setForeignKey('patient_id')
        ->setJoinType('INNER');
    }
}
$this->paginate = ['contain' => ['Patients', 'MadicalCases']];

$events = $this->Events->find('all')->where(['Events.patient_id' => $patient_id])->where(['Events.madical_case_id' => $madical_case_id]);

$allevents = $this->paginate($events);