MVC, where do the classes go?

My understanding of MVC is as follows (if it is terribly mistaken, I will be new after it)

  • Models are the things that interact with the database.
  • Views are design / page layout
  • Controllers where it all starts and is essentially the logic of the page

I use CodeIgniter , but I would venture to suggest that it is not limited to this, or perhaps even just to the PHP frameworks.

Where do I put global classes?

I can have a model for the products, and then I run a query that collects 20 products from the database. Can I create 20 models now or do I have a separate class for it, if the last one is where I put this class (other controllers will have to use it)

+5
source share
5 answers

A model is the wrong word to use when discussing what to do with products: each product is a value (VO) object (or a data transfer / DTO object that works best for your mouth). Value objects usually have the same fields as the table. In your case, ProductVO should have fields that are in the Products table.

A model is a data access object (DAO) that has methods of type

findByPk --> returns a single value object
findAll --> returns a collection of value objects (0-n)
etc.

In your case, you will have a ProductDAO that has something like the above methods. This ProductDAO will then return ProductVO and their collections.

- (BO), VO , .

: ProductDAO, . ProductVO (s) ( Java). / productVO.

+4

, -. , : , , , . CAN DO , .

- , . View , , , (X/HTML, XML, RSS/Atom, CSV).

- , . , , . , - , .

. , . 20 . -, .

+3

CakePHP "":

, , . , CodeIgniter , , . .

( , ).

+1

:

  • . , .
  • /, , com.company.model(Java/#)
  • DAO , com.company.model.dao
  • //. List <Product> .
  • , PHP. Dunno, , , , .
+1

@ , CakePHP. . , , - ​​ . , , :

  • Lockable
  • Tagable
  • Commentable

.

, MVC, , , , , - , .. CakePHP .

, CakePHP, .

I suspect CodeIgniter does not have such a flexible structure, it is smaller and lighter than CakePHP, but take a quick look at the CakePHP Guide to see how the Behaviors, Components, Helpers, and Vendors folders can be useful.

It's nice to just include some common helper classes from your models so that they are nice and DRY

+1
source

All Articles