Php 在同一控制器中的函数之间传递变量
我已经通过从DB获取值设置了$stats,我想在同一控制器的另一个函数中使用它 那么我想用这个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'));
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.
?>