Displaying a specific row in another controller when redirected from one controller

120 views Asked by At

I am creating a website of Hospital Management system. I have a Patients controller and a Medical Report controller. Every patient has an action of "View Report". When the user clicks on view report, he/she should be directed to the Medical Report and only that field pertaining to the Patient_id in Patients Controller should be displayed. How do I go about it?

Patients table:

<?php
namespace App\Model\Table;

use Search\Manager;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class PatientsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('patients');
        $this->displayField('Patient_ID');
        $this->primaryKey('Patient_ID');
        $this->addBehavior('Search.Search');
        $this->searchManager()
             ->value('Patient_ID');
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->allowEmpty('Patient_ID', 'create');

        $validator
            ->requirePresence('Name', 'create')
            ->notEmpty('Name');

        $validator
            ->requirePresence('Address', 'create')
            ->notEmpty('Address');

        $validator
            ->date('DOB')
            ->requirePresence('DOB', 'create')
            ->notEmpty('DOB');

        $validator
            ->allowEmpty('Contact');

        $validator
            ->requirePresence('Gender', 'create')
            ->notEmpty('Gender');

        $validator
            ->allowEmpty('Blood_Group');

        return $validator;
    }
}

MedicalReport table:

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class MedicalReportTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('medical_report');
        $this->displayField('Report_No');
        $this->primaryKey('Report_No');
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->requirePresence('Patient_ID', 'create')
            ->notEmpty('Patient_ID');

        $validator
            ->requirePresence('Report_No', 'create');

        $validator
            ->date('R_date')
            ->requirePresence('R_date', 'create')
            ->notEmpty('R_date');

        $validator
            ->date('C_date')
            ->requirePresence('C_date');

        $validator
            ->requirePresence('Room_No');

        $validator
            ->allowEmpty('Diet');

        $validator
            ->numeric('Payment')
            ->requirePresence('Payment');

        return $validator;
    }
}
1

There are 1 answers

0
Sevvlor On

You can use CakePHP's HtmlHelper to create an URL to view the Medical Report like so:

<?php
echo $this->Html->link(
    'View Report',
    ['controller' => 'MedicalReports', 'action' => 'view', $MedicalReport->id],
    ['class' => 'button', 'target' => '_blank']);

This would create something like: <a href="/medical-reports/view/123" class="button" target="_blank">View Report</a>

General purpose method for creating HTML links. Use $options to specify attributes for the element and whether or not the $title should be escaped.

Read more about the HtmlHelper::link();

MedicalReportsController.php

In your controller you can create the view() function that defines the code for the action.

 <?php // src/Controller/MedicalReportsController.php

class MedicalReportsController extends AppController {
    public function view($id)
    {
        // Fetch the medical report from the database based on ID
        $medicalReport = $this->MedicalReports->get($id);

        // Pass it along to the view
        $this->set('medical_report',$medicalReport);        
    } }

The set() function sends data to the View from the Controller.

The Controller::set() method is the main way to send data from your controller to your view.