CakePHP 调用非对象的成员函数 disable()

CakePHP Call to a member function disable() on a non-object

以前从未使用过 cakephp 我们有一个项目要为我们的客户编辑 cakephp 2.5.

我们尝试创建一个新功能“Emplois”以在网站内添加职位。我们主要重新创建已经在网站内运行的其他模型/控制器中存在的内容,但目前我们遇到此错误,我们无法解决问题。

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 为空,我们不知道如何 link 模型控制器。

这里是/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;

    }  
    
}

如评论中所述,您的代码的问题在于它使用了非美国英语名称,这违反了 CakePHP 的命名约定,并破坏了使用变形来完成查找和加载默认模型等任务的魔力对于基于单数化控制器名称的控制器。

词形变化主要适用于基于美国英语的词的词尾,词形变化器并不了解所有可能的词,它只是一些基本的语法规则和一些例外情况,因此可能适用于其他语言的词仅仅是因为他们的结局恰好符合规则。 -ois 因为在您的 Emplois 名称中匹配未变形词的规则,它会捕获像 bourgeoisIllinois 这样的词。关于评论 ProjetsProjects 中的示例,变形器没有区别,它不会匹配任何不规则的东西,而只是匹配 -s 结束规则,因此被解释为复数,所以它恰好只是偶然起作用,可以这么说。

理想情况下,您到处都使用正确的英文名称。如果你现在不能解决这个问题,那么下一个最好的办法就是使用自定义变形,或者根据变形失败的情况,手动进行干预,比如手动使用 loadModel()Emploi/Emplois 的自定义变形示例为:

// in `app/Config/bootstap.php`

Inflector::rules('singular', array(
    'irregular' => array(
        'emplois' => 'emploi',
    ),
));

另见