【发布时间】: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