2013-08-07 77 views
-1

Neebe在cakephp試圖建立一個簡單的appoiment系統。 我的問題是隻填寫屬於user.But當前日誌pacients名稱的Pacient名稱,但如果我使用find('list'),我得到所有用戶的所有pacients。CakePhp belongsTo輸入

這裏是有問題的數據庫表;

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 - 用戶

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 - 約會

CREATE TABLE IF NOT EXISTS `Doctor_Appointments`.`appointments` (
`id` CHAR(36) NOT NULL , 
`pacient_id` VARCHAR(45) NULL , 
`date` DATETIME NULL 

Pacient型號

public $belongsTo = array(
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

Appoiment型號

public $belongsTo = array(
    'Pacient' => array(
     'className' => 'Pacient', 
     'foreignKey' => 'pacient_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 

約會/ 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'); 

的問題必須是在此間舉行的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')); 

} 

公關($ pacients)給我太大木柵數據看到它下面

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 
      (
      ) 

    ) 

回答

0

Figuere出來後試圖許多出土文物和Pr

$pacients =$this->Appointment->Pacient->find('list', array(
     'conditions' => array('User_id' => $foo) 
    )); 

我希望它能幫助像我這樣的另一個neeby