我正在使用Cakephp,当我从另一个Controller调用方法时,会出现会话错误。
错误
public interface IndexRepository extends CrudRepository<Index, Long> {
Index findFirstByOrderByIdAsc();
}
控制器酒店
Error: Call to a member function read() on a non-object
控制器报告方法
App::import('Controller', 'Reports');
$Report = new ReportsController;
$Report->hotelsActivity($cabecera, $data, "Actividad Hotels Report");
标头appController
function hotelsActivity($cabecera, $data, $nombre){
$this->layout = false;
$this->autoRender = false;
$size = parent::excel($cabecera, $data, $nombre);
$this->set('filesize',CakeNumber::toReadableSize($size));
$this->set('counter',count($data));
$this->render('new_reports');
}
Line on在父控制器中获取错误
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Session'
);
public $helpers = array(
'Session'
);
public function beforeFilter() {
parent::beforeFilter();
//$this->Auth->allow();
//$this->Auth->allow("*");
}
public function breadcump(){
$model = $this->name;
$breadcump = Array();
foreach($_GET as $key => $value){
switch($key){
case "hotels_id":
$breadcump_model = "Hotel";
$breadcump_name = "Hotel";
$breadcump_field = "name";
break;
case "medics_id":
$breadcump_model = "Medic";
$breadcump_name = "Médico";
$breadcump_field = "name";
break;
case "users_id":
$breadcump_model = "User";
$breadcump_name = "Usuario";
$breadcump_field = "user_name";
break;
case "manager_id":
$breadcump_model = "User";
$breadcump_name = "Manager";
$breadcump_field = "user_name";
break;
case "group_id":
$breadcump_model = "Group";
$breadcump_name = "Grupo";
$breadcump_field = "name";
break;
case "nightusers_id":
$breadcump_model = "User";
$breadcump_name = "Responsable de noche";
$breadcump_field = "user_name";
break;
case "hotelstypes_id":
$breadcump_model = "Hotelstype";
$breadcump_name = "Tipo";
$breadcump_field = "name";
break;
case "hotelsgroups_id":
$breadcump_model = "Hotelsgroup";
$breadcump_name = "Cadena";
$breadcump_field = "name";
break;
case "regions_id":
$breadcump_model = "Region";
$breadcump_name = "Region";
$breadcump_field = "name";
break;
case "zones_id":
$breadcump_model = "Zone";
$breadcump_name = "Zona";
$breadcump_field = "name";
break;
case "subzones_id":
$breadcump_model = "Subzone";
$breadcump_name = "Subzona";
$breadcump_field = "name";
break;
case "centres_id":
$breadcump_model = "Centre";
$breadcump_name = "Centro";
$breadcump_field = "name";
break;
case "ttoos_id":
$breadcump_model = "Ttoo";
$breadcump_name = "Tour Operador";
$breadcump_field = "name";
break;
case "incidencestypes_id":
$breadcump_model = "Incidencestype";
$breadcump_name = "Tipo";
$breadcump_field = "name";
break;
case "origins_id":
$breadcump_model = "Origin";
$breadcump_name = "Origen";
$breadcump_field = "name";
break;
case "ttoogroups_id":
$breadcump_model = "Ttoogroup";
$breadcump_name = "Grupo";
$breadcump_field = "name";
break;
case "ttoonationalities_id":
$breadcump_model = "Ttoonationality";
$breadcump_name = "Nacionalidad";
$breadcump_field = "name";
break;
default:
$breadcump_model = null;
}
// Condición para las búsquedas belongsto
if($breadcump_model){
if($value != ""){
$breadcump[$key] = $value;
$this->loadModel($breadcump_model);
$conditions = array('conditions' => array($breadcump_model.'.id =' => $value), 'fields' => array($breadcump_model.'.'.$breadcump_field));
$hotel = $this->$breadcump_model->find('all', $conditions);
if($breadcump_name == "Responsable de noche"){
$breadcump[$breadcump_model."2"]['data_name'] = $breadcump_name;
$breadcump[$breadcump_model."2"]['data'] = $hotel[0][$breadcump_model][$breadcump_field];
$breadcump[$breadcump_model."2"]['id'] = $value;
}
else{
$breadcump[$breadcump_model]['data_name'] = $breadcump_name;
$breadcump[$breadcump_model]['data'] = $hotel[0][$breadcump_model][$breadcump_field];
$breadcump[$breadcump_model]['id'] = $value;
}
}
}
// Condición para las búsquedas fijas tipo fecha inicio, fecha fin, status..
else{
if($value != ""){
if($key == "status"){
if ($model=="Incidences") {
$key = "Estado";
switch($value){
case 1: $value = "Activa"; break;
case 2: $value = "Finalizada"; break;
}
} else {
$key = "Estado";
switch($value){
case 1: $value = "Aceptada"; break;
case 2: $value = "Asignada"; break;
case 3: $value = "Registrada"; break;
case 4: $value = "Anulada"; break;
}
}
}
if($key == "colaborator"){
switch($value){
case 'y': $value = "Sí"; break;
case 'n': $value = "No"; break;
}
}
if($key == "priority"){
$key = "Prioridad médico";
}
if($key =="dpd1"){
$key = "Fecha Inicio";
}
if($key =="dpd2"){
$key = "Fecha Fin";
}
$breadcump[$key] = $value;
$breadcump[$key] = array('data_name' => $key, 'data' => $value);
}
}
}
return $breadcump;
}
public function excel($cabecera, $data, $report_type){
$counter = count($data);
$celdas = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
// Importamos la clase PHPExcel
App::import('Vendor', 'Classes/PHPExcel');
App::uses('CakeTime', 'Utility');
App::uses('CakeNumber', 'Utility');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$session = $this->request->session();
$session->read('auth_user.User.user_name');
// Set properties
$objPHPExcel->getProperties()->setCreator($session->read('auth_user.User.user_name'));
$objPHPExcel->getProperties()->setLastModifiedBy($this->Session->read('auth_user.User.user_name'));
$objPHPExcel->getProperties()->setTitle("FreepApp ".$report_type);
$objPHPExcel->getProperties()->setSubject("Autogenerated Excel File");
$objPHPExcel->getProperties()->setDescription("This is an automated excel file generated by IMS FreepApp Application");
$filename = WWW_ROOT.'/files/tmp_report_'.$this->Session->read('auth_user.User.id').'.xlsx';
$this->set('filename','tmp_report_'.$this->Session->read('auth_user.User.id').'.xlsx');
//Sheet rename
$objPHPExcel->getActiveSheet()->setTitle($report_type);
//Creating Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', Configure::read('title') . ' ' . Configure::read('Ver') . ' Report System');
$objPHPExcel->getActiveSheet()->SetCellValue('A2', 'Type');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', $report_type);
$objPHPExcel->getActiveSheet()->SetCellValue('A3', 'Records');
$objPHPExcel->getActiveSheet()->SetCellValue('B3', $counter);
$objPHPExcel->getActiveSheet()->SetCellValue('A4', 'Date');
$objPHPExcel->getActiveSheet()->SetCellValue('B4', date('d/m/Y H:i:s', time()));
$objPHPExcel->getActiveSheet()->SetCellValue('A5', 'User');
$objPHPExcel->getActiveSheet()->SetCellValue('B5', $this->Session->read('auth_user.User.user_name'));
$objPHPExcel->getActiveSheet()->mergeCells('A1:X1');
$objPHPExcel->getActiveSheet()->mergeCells('B2:X2');
$objPHPExcel->getActiveSheet()->mergeCells('B3:X3');
$objPHPExcel->getActiveSheet()->mergeCells('B4:X4');
$objPHPExcel->getActiveSheet()->mergeCells('B5:X5');
//Header styles
$style1 = array(
'font' => array(
'bold' => true,
'size' => '18',
'color' => array('rgb' => 'ffffff')
),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_LEFT,),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => '428bca')
)
);
$style2 = array(
'font' => array(
'bold' => true,
'size' => '14',
'color' => array('rgb' => 'ffffff')
),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_LEFT,),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => '428bca')
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1:X1')->applyFromArray($style1);
$objPHPExcel->getActiveSheet()->getStyle('A2:B5')->applyFromArray($style2);
//Title Bars
for ($i=0; $i < count($cabecera); $i++) {
if($i == 0){ $primeraCelda = "$celdas[$i]7"; }
elseif( $i == (count($cabecera)-1) ){ $ultimaCelda = "$celdas[$i]7"; }
$objPHPExcel->getActiveSheet()->SetCellValue("$celdas[$i]7", "$cabecera[$i]");
}
$header = "$primeraCelda:$ultimaCelda";
$style = array(
'font' => array('bold' => true,),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'E0E0E0')
)
);
$objPHPExcel->getActiveSheet()->getStyle($header)->applyFromArray($style);
$objPHPExcel->getActiveSheet()->setAutoFilter("$primeraCelda:$ultimaCelda");
$rowCount = 8;
for ($i=0; $i < count($data); $i++) {
$contadorCeldas = 0;
foreach ($data[$i] as $key => $arrayFinal) {
$objPHPExcel->getActiveSheet()->SetCellValue("$celdas[$contadorCeldas]$rowCount", $data[$i][$key]);
$contadorCeldas++;
}
$rowCount++;
}
//Set column autosize
for($col = 'A'; $col !== 'AA'; $col++) {
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename);
$f = fopen($filename, 'r+');
$stat = fstat($f);
return $size = $stat['size'];
}
}
答案 0 :(得分:0)
请试试这个:
将组件添加到控制器
public $components = array('Session');
在你的行动中:
$session = $this->request->session();
$session->read('auth_user.User.user_name');
答案 1 :(得分:-1)
在app控制器中定义sessionhelper:
class AppController extends Controller {
public $components = array(
'Session'
);
public $helpers = array(
'Session'
);
}
然后试一试:
$session = $this->request->session();
$session->read('auth_user.User.user_name');