【发布时间】:2022-01-05 21:20:29
【问题描述】:
以前从未使用过 cakephp,我们有一个项目要为我们的客户在 cakephp 2.5 中编辑。
我们尝试创建一个新功能“Emmplois”来在网站内添加工作。我们主要从网站内已经运行的其他模型/控制器重新创建存在的内容,但目前我们收到此错误,我们无法解决问题。
Error: Call to a member function disable() on a non-object
File: /home/voyou/subdomains/labsurface/app/Controller/EmploisController.php
Line: 95
这是模型:
/app/Model/Emploi.php
<?php
App::uses('AppModel', 'Model');
/**
* Emploi Model
*
*/
class Emploi extends AppModel {
public $faIcone = "thumb-tack";
public $order = 'Emploi.ordre';
public $actsAs = array(
'I18n' => array(
'fields' => array('nom')
),
);
/**
* Display field
*
* @var string
*/
public $displayField = 'nom_fre';
/**
* Search field
*
* @var string
*/
public $searchField = 'nom_fre';
}
这是控制器:/app/Controller/EmmploisController.php
<?php
App::uses('AppController', 'Controller');
/**
* Emplois Controller
*
* @property Emploi $Emploi
*/
class EmploisController extends AppController {
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('get');
}
/**
* get method
*
* @return void
*/
public function get($id = null) {
if($id) {
return $this->Emploi->findById($id);
}
else {
return $this->Emploi->find('all');
}
}
/**
* get beforeRender method
*
* @return void
*/
public function beforeRender() {
$this->set('faIcone', $this->Emploi->faIcone);
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Emploi->recursive = 0;
$this->set('Emplois', $this->paginate());
parent::beforeRender();
}
/**
* voir method
*
* @param string $id
* @return void
*/
public function voir($id = null) {
$this->Emploi->id = $id;
if (!$this->Emploi->exists()) {
throw new NotFoundException(__('Emploi invalide'));
}
$emploi = $this->Emploi->read(null, $id);
$this->set('Emploi', $emploi);
$this->set("title_for_layout", $emploi['Emploi']['seo_title']);
$this->set("description_for_layout", $emploi['Emploi']['seo_description']);
}
/**
* page_plugin method
*
* @return void
*/
public function page_plugin($page_id = null) {
$this->Emploi->recursive = 0;
$this->set('Emplois', $this->Emploi->find('all', array('conditions'=> array('Emploi.page_id' => $page_id))));
}
/**********************************
*
* Actions administratives
*
**********************************/
/**
* admin method
*
* @return void
*/
public function admin($search = null) {
//(debug($this));
$this->layout = 'admin';
$this->Emploi->Behaviors->disable('I18n');
$this->Emploi->recursive = 0;
$options['limit'] = 10000;
if ($search) {
$options['conditions'] = array('Emploi.'.$this->Emploi->searchField.' LIKE' => '%'.$search.'%');
}
$this->paginate = $options;
$this->set('Emplois', $this->paginate());
$this->set('searchField', $this->Emploi->searchField);
if($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
$this->render('/Elements/tables/Emplois');
}
}
/**
* irre method
*
* @return void
*/
public function irre($page_id = null){
$this->set('page_id', $page_id);
$this->set('Emplois', $this->Emploi->findAllByPageId($page_id));
}
/**
* ajouter method
*
* @return void
*/
public function ajouter() {
$this->layout = 'admin';
$this->Emploi->Behaviors->disable('I18n');
if ($this->request->is('post')) {
$this->Emploi->create();
if ($this->Emploi->save($this->request->data)) {
$this->Session->setFlash(__('Le/la emploi a bien été ajouté'));
$this->redirect(array('action' => 'admin'));
} else {
$this->Session->setFlash(__('Le/la emploi n\'a pas pu être ajouté. S\'il vous plaît, essayer de nouveau.'));
}
}
$this->render('modifier');
}
/**
* modifier method
*
* @param string $id
* @return void
*/
public function modifier($id = null) {
$this->layout = 'admin';
$this->Emploi->Behaviors->disable('I18n');
$this->Emploi->id = $id;
if (!$this->Emploi->exists()) {
throw new NotFoundException(__('emploi invalide'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Emploi->save($this->request->data)) {
$this->Session->setFlash(__('Le/la emploi a bien été sauvegardé'));
$this->redirect(array('action' => 'admin'));
} else {
$this->Session->setFlash(__('Le/la emploi n\'a pas pu être sauvegardé. S\'il vous plaît, essayer de nouveau.'));
}
} else {
$this->request->data = $this->Emploi->read(null, $id);
}
}
/**
* supprimer method
*
* @param string $id
* @return void
*/
public function supprimer($id = null) {
if (!$id) {
throw new MethodNotAllowedException();
}
$this->Emploi->id = $id;
if (!$this->Emploi->exists()) {
throw new NotFoundException(__('Emploi invalide'));
}
if ($this->Emploi->delete()) {
$this->Session->setFlash(__('Emploi supprimé'));
$this->redirect(array('action' => 'admin'));
}
$this->Session->setFlash(__('Emploi n\'a pu être supprimé'));
$this->redirect(array('action' => 'admin'));
}
/**
* ordre method
*/
public function ordre() {
$this->autoRender = false;
$this->Emploi->Behaviors->disable('I18n');
$this->Emploi->Behaviors->disable('Image');
$error = 0;
Configure::write('debug', 0);
foreach($_POST['Emplois'] as $pos=>$id) {
if($id) {
$data['Emploi']['id'] = $id;
$data['Emploi']['ordre'] = $pos;
if (!$this->Emploi->save($data, array('fieldList'=>array('ordre')))) {
$error++;
}
}
}
}
}
错误指向public function admin 内的$this->Emploi->Behaviors->disable('I18n'); 行,但据我们了解,$this->Emploi 为空,我们不知道如何将模型链接到控制器。
这里是/app/Model/AppModel.php
<?php
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Model
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Model', 'Model');
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package app.Model
*/
class AppModel extends Model {
function getNextId(){
$next_increment = 0;
$table = Inflector::tableize($this->name);
$query = "SHOW TABLE STATUS LIKE '$table'";
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$result = $db->rawQuery($query);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$next_increment = $row['Auto_increment'];
}
return $next_increment;
}
}
【问题讨论】:
-
我认为这不是
$this->Emploi,而是$this->Emploi->Behaviors那是空的。你应该很容易检查这个。 -
我对 $this->Emploi 进行了调试,但遗憾的是它返回 null
(debug($this->Emploi));并且所有其他调用(如$this->Emploi->create())都无法正常工作/同样工作 -
那么,
$this->Emploi->findById($id)在get函数中也失败了?您的原始帖子使错误看起来非常特定于一个控制器功能。 -
我从未使用过 Cake v2(直接从 1.3 到 3.0),但似乎这一切都应该没问题。您是否有其他控制器可以正确加载默认模型?我想知道这是否是某种变形器问题,而不是从“Emmploi”控制器中获取“Emploi”作为表名。外语确实有时会绊倒它。您可以尝试显式调用
$this->loadModel('Emploi')并查看是否正确设置了$this->Emploi。这不是一个很好的长期解决方案,但有助于缩小问题范围。 -
因为正如你所怀疑的,the Inflector cannot singularize
Emplois,它是基于美国英语的。 Custom inflections 可以提供帮助,但最好不要使用本地化名称。
标签: php cakephp model controller cakephp-2.x