简体   繁体   English

CakePHP 3.X模型的用法

[英]CakePHP 3.X Models Usage

So, I am a pretty new PHP developer and have little idea how to use Models in CakePHP. 因此,我是一个相当新的PHP开发人员,对如何在CakePHP中使用模型一无所知。 I come from a strong Android and java background so i am very familiar with the MVC framework but after searching and digging through other people's php, i find that the usage of the model varies a lot. 我来自强大的Android和Java背景,因此我对MVC框架非常熟悉,但是在搜索并挖掘了其他人的php之后,我发现该模型的用法差异很大。 I want to get a solid answer. 我想得到一个可靠的答案。

Would i use the models in CakePhp the same as i would a class in java (create instances) and also as a connection to a database? 我会在CakePhp中使用与在Java中创建类(创建实例)相同的模型,还是与数据库建立连接? Or would i separate models from "java classes" and have different uses for both. 还是我将模型与“ java类”分开,并且两者都有不同的用途。

Thanks in advance. 提前致谢。

Model is a layer 模型是一层

Model doesn't refer to a single type of class, it's a pretty high level term an describes the layer in MVC that should implement all your data processing / fetching. 模型不是指单一类型的类,它是一个相当高级的术语,它描述了MVC中应该实现所有数据处理/获取的层。 Depending on the framework each of it has it's own structure and kind of model layer, they all differ more or less. 根据框架的不同,每个框架都有其自己的结构和模型层的种类,它们或多或少地有所不同。

Basically this can be simplified by "Fat models skinny controllers". 基本上,这可以通过“胖模型瘦控制器”简化。 Your controller should handle the request and pass required info to the model layer and get the data back it needs to display or do something else with it. 您的控制器应处理该请求,并将所需的信息传递给模型层,并取回显示或执行其他操作所需的数据。

In CakePHP 在CakePHP中

Would i use the models in CakePhp the same as i would a class in java (create instances) and also as a connection to a database? 我会在CakePhp中使用与在Java中创建类(创建实例)相同的模型,还是与数据库建立连接? Or would i separate models from "java classes" and have different uses for both. 还是我将模型与“ java类”分开,并且两者都有不同的用途。

I really recommend you to do the basic tutorial and read through the ORM section of the book . 我真的建议您做基础教程,并通读本书的ORM部分。

You can put whatever you want in the model layer. 您可以将所需的任何内容放入模型层。 For example I have classes that just return some data and others that contain static data for navigation items for example. 例如,我有一些只返回一些数据的类,而另一些则包含用于导航项的静态数据。 But lets stick to table objects for now. 但是现在让我们坚持表对象。 In cake a table object contains a reference to a connection object that contains the actual connection. 在cake中,表对象包含对包含实际连接的连接对象的引用。 Explaining this here doesn't make any sense, read the ORM section. 在这里解释这个没有任何意义,请阅读ORM部分。 :) :)

I have no idea what exactly you want to do so I can't give you any more detailed advice on what you can or should do. 我不知道您到底想做什么,所以我无法就您可以或应该做什么提供更详细的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM