大家好,祝复活节快乐!
将遗留应用程序从CakePHP 2.x迁移到CakePHP 3.x
时遇到此问题我正在尝试在模型的初始化函数(ConfigurationsTable.php - 制作成Singleton)类中加载数据库表的所有内容。我也在构造函数中尝试了相同的代码但仍然得到相同的错误。还尝试将其移至单独的功能但仍然没有运气。
它在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');
有人可以为我提供解决方案吗? 我需要它来工作..
提前多多感谢
答案 0 :(得分:-1)
你不能盲目地应用2.x概念并期望它能够发挥作用。在开始使用您不了解的代码之前,请至少查看一下API文档
<强> http://api.cakephp.org/3.0/class-Cake.ORM.Table.html#___construct 强>
并查看源本身以了解代码实际执行的操作会更好。
手动实例化表类时,必须至少提供connection instance。但是,你在那里做的事情并没有多大意义,并不需要自定义的静态实例getter,这是TableRegistry::get()
的用途。