如果这最终成为一个新的PHP误解我很抱歉,但无论如何,我有以下Vip.php
文件:
<?php
class Vip extends AppModel {
public $useTable = 'vips';
public $hasOne = 'VipCredential';
public $belongsTo = array('User', 'MasterExhibitor');
public $validate = array();
}
class InviteVipForm extends Vip {
}
class AccreditVipForm extends Vip {
}
因此,InviteVipForm
和AccreditVipForm
类都应该从父级继承每个模型属性,对吧?这不会发生。我在控制器中加载了InviteVipForm
并打印出来。一切都是空的或设置为默认值(useTable
,例如,设置为invite_vip_forms
,这当然是一个不存在的表格。
AppModel Object ( [useDbConfig] => default [useTable] => invite_vip_forms [id] => [data] => Array ( ) [schemaName] => [table] => invite_vip_forms [primaryKey] => id [_schema:protected] => [validate] => Array ( ) [validationErrors] => Array ( ) [validationDomain] => [plugin] => [name] => InviteVipForm [alias] => InviteVipForm [tableToModel] => Array ( [invite_vip_forms] => InviteVipForm ) [cacheQueries] => [belongsTo] => Array ( ) [hasOne] => Array ( ) [hasMany] => Array ( ) [hasAndBelongsToMany] => Array ( ) [actsAs] => [Behaviors] => BehaviorCollection Object ( [modelName] => InviteVipForm [_methods:protected] => Array ( ) [_mappedMethods:protected] => Array ( ) [_enabled:protected] => Array ( ) [_loaded:protected] => Array ( ) [defaultPriority] => 10 ) [whitelist] => Array ( ) [cacheSources] => 1 [findQueryType] => [recursive] => 1 [order] => [virtualFields] => Array ( ) [_associationKeys:protected] => Array ( [belongsTo] => Array ( [0] => className [1] => foreignKey [2] => conditions [3] => fields [4] => order [5] => counterCache ) [hasOne] => Array ( [0] => className [1] => foreignKey [2] => conditions [3] => fields [4] => order [5] => dependent ) [hasMany] => Array ( [0] => className [1] => foreignKey [2] => conditions [3] => fields [4] => order [5] => limit [6] => offset [7] => dependent [8] => exclusive [9] => finderQuery [10] => counterQuery ) [hasAndBelongsToMany] => Array ( [0] => className [1] => joinTable [2] => with [3] => foreignKey [4] => associationForeignKey [5] => conditions [6] => fields [7] => order [8] => limit [9] => offset [10] => unique [11] => finderQuery ) ) [_associations:protected] => Array ( [0] => belongsTo [1] => hasOne [2] => hasMany [3] => hasAndBelongsToMany ) [__backAssociation] => Array ( ) [__backInnerAssociation] => Array ( ) [__backOriginalAssociation] => Array ( ) [__backContainableAssociation] => Array ( ) [__safeUpdateMode] => [_insertID:protected] => [_sourceConfigured:protected] => [findMethods] => Array ( [all] => 1 [first] => 1 [count] => 1 [neighbors] => 1 [list] => 1 [threaded] => 1 ) [_eventManager:protected] => [_validator:protected] => )
有人能告诉我为什么会这样吗?这些课程不需要在单独的文件中,是吗?
答案 0 :(得分:2)
这不是事情的工作原理,每个模型都必须放在一个单独的文件中,否则CakePHP可能无法找到/加载它们,然后你最终得到AppModel
类的实例正在使用,这是你正在经历的。
AppModel
实例当然不会包含您在扩展类中定义的任何属性或方法。
“食谱”中也提到了这种行为,请参阅 http://book.cakephp.org/2.0/en/models.html
如果找不到
/app/Model
中的相应文件,CakePHP将为您动态创建一个模型对象。这也意味着如果您的模型文件未正确命名(例如,如果它被命名为ingredient.php
或Ingredients.php
而不是Ingredient.php
),CakePHP将使用{{1而不是你的模型文件(CakePHP假设缺少)。如果你试图使用你在模型中定义的方法,或者附加到模型的行为,并且你得到的SQL错误是你正在调用的方法的名称,那么它肯定是CakePHP的标志无法找到您的模型,您需要检查文件名,应用程序缓存或两者。