CakePhp belongsTo input
Neebe in cakephp trying to set up a simple appoiment system. My problem is
to populate the Pacient Name with only the names of the the pacients
belonging to the current log in user.But if I used find('list') I get all
the pacients for all the users.
here are the database table in question;
1 - pacients
CREATE TABLE IF NOT EXISTS `Doctor_Appointments`.`pacients` (
`id` CHAR(36) NOT NULL ,
`name` VARCHAR(45) NULL ,
`user_id` CHAR(36) NOT NULL ,
2 - Users
CREATE TABLE IF NOT EXISTS `Doctor_Appointments`.`users` (
`id` CHAR(36) NOT NULL ,
`username` VARCHAR(65) NULL ,
`email` VARCHAR(65) NULL ,
`password` VARCHAR(65) NULL ,
3 - Appointments
CREATE TABLE IF NOT EXISTS `Doctor_Appointments`.`appointments` (
`id` CHAR(36) NOT NULL ,
`pacient_id` VARCHAR(45) NULL ,
`date` DATETIME NULL
Pacient Model
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Appoiment Model
public $belongsTo = array(
'Pacient' => array(
'className' => 'Pacient',
'foreignKey' => 'pacient_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
Appointments/add.ctp
<?php echo $this->Form->create('Appointment'); ?>
<fieldset>
<legend><?php echo __('Add Appointment'); ?></legend>
<?php
echo $this->Form->input('date');
echo $this->Form->input('pacient_id');
The problem must be here at the AppointmentsController
public function add() {
if ($this->request->is('post')) {
$this->Appointment->create();
$this->request->data['appointment']['user_id'] =
$this->Auth->user('id'); //Added this line
if ($this->Appointment->save($this->request->data)) {
$this->Session->setFlash(__('The appointment has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The appointment could not be
saved. Please, try again.'));
}
}
$foo = $this->Auth->user('id');
$pacients = $this->Appointment->Pacient->findAllByUserId($foo,
array('Pacient.name') , array("Pacient.name" => 'desc') );
pr($pacients);
$doctors = $this->Appointment->Doctor->find('list');
$this->set(compact('pacients', 'doctors'));
}
The pr($pacients) give me too mucha data see it below
Array
(
[0] => Array
(
[Pacient] => Array
(
[name] => samuel Giani
[id] => 520178db-aa94-40d2-86e9-4fc38e7a58df
)
[Appointment] => Array
(
)
)
[1] => Array
(
[Pacient] => Array
(
[name] => pablo Giani
[id] => 520178ed-d564-465c-93fd-498f8e7a58df
)
[Appointment] => Array
(
)
)
)
No comments:
Post a Comment