Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在同一控制器中的函数之间传递变量_Php_Variables_Cakephp - Fatal编程技术网

Php 在同一控制器中的函数之间传递变量

Php 在同一控制器中的函数之间传递变量,php,variables,cakephp,Php,Variables,Cakephp,我已经通过从DB获取值设置了$stats,我想在同一控制器的另一个函数中使用它 那么我想用这个 class AppointmentsController extends AppController { public function view($id = null) { if (!$this->Appointment->exists($id)) { throw new NotFoundException(__('Invalid appointment'));

我已经通过从DB获取值设置了$stats,我想在同一控制器的另一个函数中使用它

那么我想用这个

class AppointmentsController extends AppController {
public function view($id = null) {
    if (!$this->Appointment->exists($id)) {
        throw new NotFoundException(__('Invalid appointment'));
    }
    $options = array('conditions' => array('Appointment.' . $this->Appointment->primaryKey => $id));
    $this->set('appointment', $this->Appointment->find('first', $options));

    $kk = $this->Appointment->find('first',  array('fields' => 'status', 'conditions' => array('Appointment.id' => $id)));
    $ss = reset($kk);
    $stats = reset($ss);
}
}

看到它是同一个类,您是否尝试过使用$this->stats而不是局部变量?

您可以在第一个函数中调用另一个函数,如

class AppointmentsController extends AppController {
function confirm(){
$stats = 'New';

}
}

或者您可以将status设置为全局变量,这样您就可以在任何函数中访问它。

我认为,您必须在模型中创建函数,该函数将按id返回字段的值

$this->confirm($status); // calling confirm function

function confirm($status)
{
 //use status as you want
}

你看过类变量/属性了吗?
<?php
// model/AppModel.php

public function getFieldById($field='id', $id){
  return $this->field($field, array('id' => $id));
}


// in controller function you can access this like.

$status = $this->Appointment->getFieldById('status', $id); // $id will be appointment id.

?>