CakePHP扩展模型不继承属性

问题描述 投票:0回答:1

我很抱歉,如果这最终成为一个新的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 {

}

所以,InviteVipFormAccreditVipForm类都应该继承父母的每个模型属性,对吧?这不会发生。我在控制器中加载了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] => )

谁能告诉我为什么会这样?这些类不需要在单独的文件中,是吗?

php cakephp inheritance model
1个回答
2
投票

这不是事情的工作原理,每个模型必须放在一个单独的文件中,否则CakePHP可能无法找到/加载它们,然后你最终会得到一个正在使用的AppModel类的实例,这就是你正在经历。

当然,AppModel实例不具有您在扩展类中定义的任何属性或方法。

Cookbook中也提到了这种行为,请参阅http://book.cakephp.org/2.0/en/models.html

如果找不到/app/Model中的相应文件,CakePHP将为您动态创建一个模型对象。这也意味着如果您的模型文件未正确命名(例如,如果它被命名为ingredient.phpIngredients.php而不是Ingredient.php),CakePHP将使用AppModel的实例而不是您的模型文件(CakePHP假定缺少该实例)。如果您尝试使用模型中定义的方法,或者附加到模型的行为,并且您得到的SQL错误是您正在调用的方法的名称,那么CakePHP就是一个明确的信号。找不到您的模型,您需要检查文件名,应用程序缓存或两者。

© www.soinside.com 2019 - 2024. All rights reserved.