【问题标题】:Fatal error: Call to a member function schemaCollection() on a non-object in ...vendor/cakephp/cakephp/src/ORM/Table.php on line 421致命错误:在第 421 行对 ...vendor/cakephp/cakephp/src/ORM/Table.php 中的非对象调用成员函数 schemaCollection()
【发布时间】:2015-04-06 00:27:25
【问题描述】:

大家好,复活节快乐!

我在将遗留应用程序从 CakePHP 2.x 迁移到 CakePHP 3.x 时遇到了这个问题

我试图在模型(ConfigurationsTable.php - 制成单例)类的初始化函数中加载数据库表的所有内容。我还在构造函数中尝试了相同的代码,但仍然得到相同的错误。还尝试将其移至单独的函数,但仍然没有运气。

在 CakePHP 2.x 中运行良好,但在 CakePHP 3 中出现致命错误。

代码如下

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

class ConfigurationsTable extends Table
{

private $_configurations;

public static function getInstance()
{
    static $instance = null;

    if ($instance === null) {
        $instance = new static();
    }

    return $instance;
}

public function is_set($key)
{
    return isset($this->_configurations->{$key});
}

public function fetch($key)
{
    return $this->_configurations->{$key};
}

public function initialize(array $config)
{
    $this->addBehavior('Timestamp');

    $this->_configurations = new \stdClass();

    $configs = $this->find('all');

    foreach ($configs as $c) {
        if (isset($c->key) && $c->key != '') {
            $this->_configurations->{$c->key} = $c->value;
        }
    }


}


public function validationDefault(Validator $validator)
{
    $validator
        ->notEmpty('key')
        ->add('key', [
            'unique' => [
                'rule' => 'validateUnique',
                'provider' => 'table',
                'message' => __('This configuration key already exists')
            ]
        ])
        ->notEmpty('value')
        ;

    return $validator;
}

导致错误的行是:$configs = $this->find('all');

谁能为此提供解决方案? 我工作需要它..

提前非常感谢

【问题讨论】:

    标签: php mysql cakephp cakephp-3.0


    【解决方案1】:

    您不能盲目地应用 2.x 概念并期望它起作用。在开始使用您不了解的代码之前,请至少查看 API 文档

    http://api.cakephp.org/3.0/class-Cake.ORM.Table.html#___construct

    查看源代码本身以了解代码的实际作用会更好。

    手动实例化表类时,至少必须提供connection instance。但是,您在那里所做的事情没有多大意义,实际上并不需要自定义静态实例 getter,这就是 TableRegistry::get() 的用途。

    【讨论】:

    • 嗨,感谢您的时间和精力以及有用的指示,我设法让它工作。再次感谢!
    猜你喜欢
    • 2021-03-06
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多