【问题标题】:CakePHP Call to a member function disable() on a non-objectCakePHP 在非对象上调用成员函数 disable()
【发布时间】: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-&gt;Emploi-&gt;Behaviors-&gt;disable('I18n'); 行,但据我们了解,$this-&gt;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-&gt;Emploi,而是$this-&gt;Emploi-&gt;Behaviors 那是空的。你应该很容易检查这个。
  • 我对 $this->Emploi 进行了调试,但遗憾的是它返回 null (debug($this-&gt;Emploi)); 并且所有其他调用(如 $this-&gt;Emploi-&gt;create())都无法正常工作/同样工作
  • 那么,$this-&gt;Emploi-&gt;findById($id)get 函数中也失败了?您的原始帖子使错误看起来非常特定于一个控制器功能。
  • 我从未使用过 Cake v2(直接从 1.3 到 3.0),但似乎这一切都应该没问题。您是否有其他控制器可以正确加载默认模型?我想知道这是否是某种变形器问题,而不是从“Emmploi”控制器中获取“Emploi”作为表名。外语确实有时会绊倒它。您可以尝试显式调用 $this-&gt;loadModel('Emploi') 并查看是否正确设置了 $this-&gt;Emploi。这不是一个很好的长期解决方案,但有助于缩小问题范围。
  • 因为正如你所怀疑的,the Inflector cannot singularize Emplois,它是基于美国英语的。 Custom inflections 可以提供帮助,但最好不要使用本地化名称。

标签: php cakephp model controller cakephp-2.x


【解决方案1】:

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

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

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

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

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

另见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多