0

我正在使用此链接中的反馈插件来使用 PolymorphicBehavior 在我的应用程序中的任何模型上添加评论。我的应用程序出现 2 个错误,上面写着

Strict (2048):PolymorphicBehavior::setup() 的声明应该与 ModelBehavior::setup(Model $model, $config = Array) 兼容 [APP\Plugin\Feedback\Model\Behavior\PolymorphicBehavior.php,第 93 行]

Strict (2048):PolymorphicBehavior::afterFind() 的声明应与 ModelBehavior::afterFind(Model $model, $results, $primary) [APP\Plugin\Feedback\Model\Behavior\PolymorphicBehavior.php,第 93 行] 兼容

不确定是什么导致了这些错误。如果有人可以帮助我,那就太棒了。这是我的 PolymorphicBehavior.php 的代码。请不要第 93 行是代码中的最后一行

<?php

App::uses('ModelBehavior', 'Model');

//PolymorphicBehavior class

class PolymorphicBehavior extends ModelBehavior {

//defaultSettings property

protected $_defaultSettings = array(
'modelField' => 'model',
'foreignKey' => 'foreign_key'
);

// setup method

public function setup($Model, $settings = array()) {
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = $this->_defaultSettings;
}
$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], (array) $settings);
}

//afterFind method

public function afterFind($Model, $results, $primary = false) {
extract($this->settings[$Model->alias]);
if (App::import('Vendor', 'MiCache')) {
$models = MiCache::mi('models');
} else {
$models = App::objects('Model');
}
if ($primary && isset($results[0][$Model->alias][$modelField]) && isset($results[0][$Model->alias][$foreignKey]) && $Model->recursive > 0) {
foreach ($results as $key => $result) {
$associated = array();
$model = Inflector::classify($result[$Model->alias][$modelField]);
$foreignId = $result[$Model->alias][$foreignKey];
if ($model && $foreignId && in_array($model, $models)) {
$result = $result[$Model->alias];
if (!isset($Model->$model)) {
$Model->bindModel(array('belongsTo' => array(
$model => array(
'conditions' => array($Model->alias . '.' . $modelField => $model),
'foreignKey' => $foreignKey
)
)));
}
$conditions = array($model . '.' . $Model->$model->primaryKey => $result[$foreignKey]);
$recursive = -1;
$associated = $Model->$model->find('first', compact('conditions', 'recursive'));
$name = $Model->$model->display($result[$foreignKey]);
$associated[$model]['display_field'] = $name?$name:'*missing*';
$results[$key][$model] = $associated[$model];
}
}
} elseif(isset($results[$Model->alias][$modelField])) {
$associated = array();
$model = Inflector::classify($result[$Model->alias][$modelField]);
$foreignId = $results[$Model->alias][$foreignKey];
if ($model && $foreignId) {
$result = $results[$Model->alias];
if (!isset($Model->$model)) {
$Model->bindModel(array('belongsTo' => array(
$model => array(
'conditions' => array($Model->alias . '.' . $modelField => $model),
'foreignKey' => $foreignKey
)
)));
}
$conditions = array($model . '.' . $Model->$model->primaryKey => $result[$foreignKey]);
$recursive = -1;
$associated = $Model->$model->find('first', compact('conditions', 'recursive'));
$name = $Model->$model->display($result[$foreignKey]);
$associated[$model]['display_field'] = $name?$name:'*missing*';
$results[$model] = $associated[$model];
}
}
return $results;
}

//display method

public function display($Model, $id = null) {
if (!$id) {
if (!$Model->id) {
return false;
}
$id = $Model->id;
}
return current($Model->find('list', array('conditions' => array($Model->alias . '.' . $Model->primaryKey => $id))));
}
}
4

1 回答 1

0

只需调整插件文件以进行建议的更改。参考实现是这样的:

// /lib/Cake/Model/ModelBehavior.php
public function setup(Model $model, $config = array()) {
public function afterFind(Model $model, $results, $primary) {

...所以你需要改变这个:

public function setup($Model, $settings = array()) {
public function afterFind($Model, $results, $primary = false) {

...进入这个:

public function setup(Model $Model, $settings = array()) {
public function afterFind(Model $Model, $results, $primary) {

如果您仍然收到太多错误,您可能需要隐藏严格的警告或切换到更好的插件。

于 2013-03-04T09:40:24.130 回答