CakePHP扩展模型不继承属性 [英] CakePHP extended model not inheriting attributes

查看:68
本文介绍了CakePHP扩展模型不继承属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这最终是对PHP的误解,我深表歉意,但是无论如何,我有以下Vip.php文件:

I apologize if this ends up being a newbish PHP misunderstanding, but anyway, I have the following Vip.php file:

<?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,这当然是不存在的表).

So, both InviteVipForm and AccreditVipForm classes should inherit every model attributes from their parent, right? This doesn't happen. I loaded InviteVipForm in the controller and printed. Everything is empty or set to default (useTable, for example, is set to invite_vip_forms, which is of course a table which doesn't exist).

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] => )

有人可以告诉我为什么会这样吗?这些类不必放在单独的文件中,对吗?

Could anybody tell me why this is happening? The classes don't need to be in separate files, do they?

推荐答案

那不是工作原理,每个模型都必须放在单独的文件中,否则CakePHP可能无法找到/加载它们,然后您就可以了.最后将使用正在使用的AppModel类的实例.

That's not how things work, each model has to be placed in a separate file, otherwise CakePHP might not be able to find/load them, and then you'll end up with an instance of the AppModel class being used, which is what you are experincing.

AppModel实例当然不会具有您在扩展类上定义的任何属性或方法.

The AppModel instance of course won't have any of the properties or methods that you've defined on your extending class.

食谱中也提到了此行为,请参见 http://book.cakephp.org/2.0/en/models.html

This behavior is also mentioned in the Cookbook, see http://book.cakephp.org/2.0/en/models.html

如果在/app/Model中找不到对应的文件,CakePHP将为您动态创建模型对象.这也意味着,如果您的模型文件命名不正确(例如,如果命名为ingredient.phpIngredients.php而不是Ingredient.php),则CakePHP将使用AppModel的实例,而不是模型文件( CakePHP假定缺少该属性).如果您尝试使用模型中定义的方法或模型附带的行为,并且遇到SQL错误(即您正在调用的方法的名称),则可以肯定地表明CakePHP找不到您的模型,您需要检查文件名,应用程序缓存或两者.

CakePHP will dynamically create a model object for you if it cannot find a corresponding file in /app/Model. This also means that if your model file isn’t named correctly (for instance, if it is named ingredient.php or Ingredients.php rather than Ingredient.php), CakePHP will use an instance of AppModel rather than your model file (which CakePHP assumes is missing). If you’re trying to use a method you’ve defined in your model, or a behavior attached to your model, and you’re getting SQL errors that are the name of the method you’re calling, it’s a sure sign that CakePHP can’t find your model and you need to check the file names, your application cache, or both.

这篇关于CakePHP扩展模型不继承属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆