-1

Cakephp 中的 Neebe 试图建立一个简单的 appoiment 系统。我的问题是仅使用属于当前登录用户的 pacients 的名称填充 Pacient 名称。但如果我使用 find('list'),我会得到所有用户的所有 pacients。

这是有问题的数据库表;

1 - 病人

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 

患者模型

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

预约模型

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

    )

)

4

1 回答 1

0

在尝试了许多发现和 Pr 之后解决它

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

我希望它可以帮助像我这样的另一个邻居

于 2013-08-07T16:00:30.247 回答