简体   繁体   中英

How to handle database logic from model

I created a enterprise system model from a class diagram and converted it php object oriented code.

What I have now is just an empty model, it's able to relate and the connections are handled. Now the next step is ahead. I have a database and have to put the object into the database.

Now there is a lack of experience on my side. I have the model and since php renders a view and makes one time requests to render the view I'm a bit confused. It can't be possible to load all my entities out of the database and fill my model in memory and then use it because it would be way to large.

Does anybody know any ways to load just the needed data in my model and then use it? Maybe suggestions of a book or article?

Thanks!

It sounds like you are running a "MV" (which doesn't exist) rather than "MVC" design pattern.

What's happened to your controller? Your controller should route the request to the model entity which is required for this particular view and then you can use the response as you require.

If your model represents information which is too large, then you are doing something wrong. You should consider lazy-loading your data, and only loading what is strictly neccessary. Naturally your controller will decide this and route everything to your model.

Edit in response to comments

You have several methods. If you are using Zend_Db_Table_Abstract then you subclass this class and use it as your model. This is probably the easiest. Then you just use it as your model. Set the $_name = "myComments" and then just create a method called getComments .

In your method on in your subclass of Zend_Db_Table_Abstract, you can then create your $select. Then you just return the response from your model.

So

    $rows = $table->fetchAll($select);

And then just return $rows; You will then get your $rows back in your controller. You have loaded your model in your controller by $model = new Comments(); and then do the fetch using $result = $model->getComments();

Its a bit of a mismatch ,but that should show you how you should do it. You just need to create your model now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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