开发者

CakePHP: Using two tables for a single model

开发者 https://www.devze.com 2022-12-31 14:31 出处:网络
I\'m just picking up development in CakePHP right now so forgive me if this seems obvious; it did to me when I first read about has, belongsTo, hasMany, etc.

I'm just picking up development in CakePHP right now so forgive me if this seems obvious; it did to me when I first read about has, belongsTo, hasMany, etc.

The problem is I would like to associate two tables with a single model, and was wondering if there was a way to configure this so that when CakePHP did it's queries it automatically performed a join on the two tables.

I don't want to create a separate model for the second table as it is merely a meta information table - the master table will contain the pri开发者_JS百科mary information required, the meta table will be populated with secondary information that is not required and therefore may or may not be set for every row of the master table.


You don't need to explicitly create a model for a table if you will always be accessing the second table through the first. If memory serves, Cake will implicitly create the model and return its data when the first is referenced using standard Cake methods. If you need custom methods, you'll need to code out the model.

For example, I often have a Company model and an Address model. I create a model for the former, but not the latter. Calling Company::find() returns the address information as long as Company hasOne Address (or hasMany) and the recursive option is set to 1 or higher.

0

精彩评论

暂无评论...
验证码 换一张
取 消