CakePhp属于输入

时间:2013-08-07 00:27:33

标签: cakephp-2.3

Neebe在cakephp试图建立一个简单的appoiment系统。 我的问题是只使用属于当前登录用户的pacients的名称来填充Pacient Name。但是如果我使用find('list')我将获得所有用户的所有pacients。

这是有问题的数据库表;

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' => ''
    )
);

Appmentment Model

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

}

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

    )

1 个答案:

答案 0 :(得分:0)

在尝试了许多发现和Pr

后计算出来
$pacients =$this->Appointment->Pacient->find('list', array(
        'conditions' => array('User_id' => $foo)
    ));

我希望它有助于像我这样的另一个新人