我从来没有使用过cakephp之前,我们有一个项目,为我们的客户编辑cakephp 2.5。
我们试图创建一个新的功能“Employis”来添加网站内的工作。我们主要是从网站内已经工作的其他模型/控制器重新创建现有的功能,但目前我们得到了这个错误,我们无法找出问题所在。
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/EmploisController.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;
}
}
1条答案
按热度按时间0qx6xfy61#
正如在注解中提到的,您的代码的问题是它使用了非美国英语名称,这违反了CakePHP的命名约定,并打破了在任务中使用屈折变化的魔力,比如基于单一化控制器名称来查找和加载控制器的默认模型。
屈折变化主要作用于美国英语单词的词尾,它并不像屈折变化知道所有可能的单词,它只是一些基本的语法规则和一些例外,所以对于其他语言的单词可能会起作用,只是因为它们的词尾恰好符合一个规则。它将捕获像
bourgeois
或Illinois
这样的单词。对于注解中的示例,Projets
和Projects
,对于偏转器没有区别,它不会匹配任何不规则的内容,而只是匹配-s
结尾规则,因此被解释为复数,所以它碰巧工作,可以这么说。理想情况下,你可以在任何地方使用正确的英语名字。如果你现在还不能解决这个问题,那么下一个最好的办法就是使用自定义的词形变化,或者根据词形变化失败的情况,手动干预,比如手动使用
loadModel()
。Emploi
/Emplois
的自定义词形变化的例子是:另请参阅
*操作手册〉开发〉配置〉转折配置