The m (model) in mvc represents the model, which is mainly used to manipulate data. So whether simple processing of data should be done in the C layer or the M layer.
<code>$sql = "SELECT id, name FROM test_table" $result = $conn->query($sql); $data = $result->getAll();</code>
In the above code, if I need to change data into an array with id as the key, should this operation be performed on layer m or layer c?
Reply content:
The m (model) in mvc represents the model, which is mainly used to manipulate data. So whether simple processing of data should be done in the C layer or the M layer.
<code>$sql = "SELECT id, name FROM test_table" $result = $conn->query($sql); $data = $result->getAll();</code>
In the above code, if I need to change data into an array with id as the key, should this operation be performed on layer m or layer c?
Personal opinion: M layer is better
Since you use MVC, it is to reduce the degree of coupling and improve scalability.
Writing mixed words is not conducive to the above two purposes.
Of course, if the project itself is not large, there is actually no need to use the mvc structure.
According to the problem in your example, I think you can use the closure function method. For example, define a dataFormat method in the model class, and then call it when returning the data. This dataFormat then uses the closure you passed in the class. function to process your return data, which is more flexible
In fact, it all works. It depends on your interface requirements. Don’t worry too much! Because of this question, I also asked my teacher. I said that the C layer is responsible for controlling page jumps and encapsulating the request data retrieval interface, while the M layer is responsible for verifying the parameters of the C layer call interface and data processing return. What is the specific business? Where to put the logic? In fact, this is all done according to the needs of our own program design
So don’t worry too much
The emergence of model is to control all the data layer in the model. Why do you do this?
<code>耦合性降低了、可读性、维护、扩展性都得到了提高。 </code>
c in mvc is used to connect model and view
The core idea of MVC is to separate the interface (View), logic (Controller), and data (Model).
For web applications, the process is C calling M and outputting V.
Page Controller:
<code>/post.php?a=create /post.php?a=read&id=1024 /post.php?a=update&id=1024 /post.php?a=delete&id=1024</code>
Model (CRUD):
<code>function post_create() {} function post_read() {} function post_update() {} function post_delete() {}</code>
View:/view/post.php
It can be seen that the CRUD operation functions for the data model post are of course summarized in /include/funclass.php
so that they can be called in other controllers. In short, if you think you need to use this operation in other controllers, Then you can consider encapsulating it into a function or class and putting it in funclass.php for "sharing".
It must be operated in the controller
Bold textPlease enter the code

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
