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

时间:2018-06-14 05:30:11

标签: cakephp cakephp-3.0

我有三张桌子

事件表中的

id
event_name
patient_id
madical_case_id
fees

在madicalCases表中

id
patient_id
case_type
date
患者表中的

id
name
address

现在我尝试用bind(madicalCases和Patients)查找所有事件。     但没有得到succsess

**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]

2 个答案:

答案 0 :(得分:1)

$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);

答案 1 :(得分:0)

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

class MadicalCasesTable extends Table{
    public function initialize(array $config){
    $this->belongsTo('Patients')
        ->setForeignKey('patient_id')
        ->setJoinType('INNER');
    }
}