我在CakePhp中创建了一个插件,我在插件中使用了多个模型。我使用控制台创建了这些模型并烘焙它们。我在使用其中一个时遇到了问题,它只出现在这个特定的模型中。
模型文件名:
的 GamificationBadgeUnlockActionProgression.php
模特内容:
<?php
App::uses('GamificationAppModel', 'Gamification.Model');
class GamificationBadgeUnlockActionProgression extends GamificationAppModel {
function __construct() {
parent::__construct();
echo 'oleej';
exit;
}
public $belongsTo = array(
'GamificationBadgeProgression' => array(
'className' => 'GamificationBadgeProgression',
'foreignKey' => 'gamification_badge_progression_id'
),
'GamificationBadgeUnlockAction' => array(
'className' => 'GamificationBadgeUnlockAction',
'foreignKey' => 'gamification_badge_unlock_action_id'
)
);
public function addMissingProgression($missingProgression)
{
/*$this->saveMany($missingProgression, array(
'fieldlist' => array('gamification_badge_progression_id', 'gamification_badge_unlock_action_id')
));*/
}
}
在插件的控制器中我有:
public $uses = array('Gamification.GamificationBadge',
'Gamification.GamificationBadgeProgression',
'Gamification.GamificationBadgeUnlockAction',
'Gamification.GamificationBadgeUnlockActionProgression',
'Gamification.GamificationBadgeUnlockType');
正在加载所有其他模型,但“Gamification.GamificationBadgeUnlockActionProgression”除外。这很奇怪。它具有所有必需的东西,它遵循命名约定和一切。 它只是没有被加载,因为从不调用构造函数,并且从不显示回显。
当我使用数组作为参数从模型中调用函数时:
$this->GamificationBadgeUnlockActionProgression->addMissingProgression($missingProgression);
然后我收到错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'addMissingProgression' at line 1
因为未加载模型 然而,CakePhp确实创建了一个具有相同名称的魔法模型,这导致了上面的错误,因为该模型神奇地激发了但是函数的功能没有,也没有关系。
我在这里缺少什么?
答案 0 :(得分:2)
当模型未正确加载时会抛出这种错误,它会尝试将该方法作为SQL函数执行,而该函数实际上并不存在。在控制器的顶部,检查它是否:
App::uses('GamificationBadgeUnlockActionProgression', 'Gamification.Model');
结合你已经拥有的$ uses数组中的Gamification.GamificationBadgeUnlockActionProgression
应该足以加载它。如果仍然失败,作为最后的手段,您可以使用后备手动在方法中手动加载它,如:
if (!is_object($this->GamificationBadgeUnlockActionProgression)) {
ClassRegistry::init('Gamification.GamificationBadgeUnlockActionProgression');
$this->GamificationBadgeUnlockActionProgression = new GamificationBadgeUnlockActionProgression();
}
这将手动加载模型并在$this->GamificationBadgeUnlockActionProgression
下为其创建实例。
模型中的关联似乎也有错误,它应该包含插件前缀,如:
public $belongsTo = array(
'GamificationBadgeProgression' => array(
'className' => 'Gamification.GamificationBadgeProgression',
'foreignKey' => 'gamification_badge_progression_id'
),
'GamificationBadgeUnlockAction' => array(
'className' => 'Gamification.GamificationBadgeUnlockAction',
'foreignKey' => 'gamification_badge_unlock_action_id'
)
);