Answer the question
In order to leave comments, you need to log in
With MVC (model) ideology in ZF1?
Good time to all, dear habravchane.
Started learning Zend Framework. The system is grandiose with its pluses, minuses, charms and complexities. Especially when you don't quite understand best practice.
I understand that I am completely confused at the end, so I turn to you for help / hint / advice, to which I will be immensely grateful.
I'm making a small REST application. A fairly standard set of simple actions, but I want to dive into all the depths of the framework and learn how to do not “how it goes”, but “how better” and “how to do it right”.
There were no issues with the controller / view. But with the model, with each article I read, I understand that I am more and more lost.
I will try to formulate the main questions.
How many files for models need to be created?
I saw different types of inheritance implementation - from _Table, _Table_Abstract, _Row, _Rowset and so on. When is it better to inherit from what?
In ideology, ZF also met the use of Mapper for models. How should the data be divided? Which part should work with the database (should it be divided into Unit / Collection, and if so, how is it better / more correct), and which one should operate with different data. For example, various fetch.
There are helpers that are used not only as assistants for view, but also for data. Is this true?
There are services that have not yet been fully mastered. Given the above questions about the model, what is better to give here?
I studied a lot of literature, but instead of understanding, there is more mess in my head. I would be glad for any help, good links / references to manuals, etc.
If the questions seem stupid, please do not downvote, but help. I really want to get better :)
Answer the question
In order to leave comments, you need to log in
A little bit of personal experience. Zend is a mega universal framework. And that means mega-large and mega-complex. And it feels like it was created by architects for architects.
Therefore, studying it, you will not find "only" (recommended) recipes for how to make this or that conceptual thing.
Therefore, your models will have several “extra” files (the same _Table, _Row, _Rowset, plus there are _Gateway, _Factory, ...)
Therefore, it will take too much time to think about how to name / make / where to put “simple” things .
Therefore, you won't be able to find a couple of dozen open projects to see how the community decided to do some thing (this thing will be done differently in the found projects).
Therefore, in the bugtracker you will see tickets open for several years with rhetorical questions "Is there any progress on this problem?".
Therefore, at first, while you have not yet mastered the framework, make it as simple as possible, without complications. If you need to make queries to the database, let it be like this for now
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$query = "SELECT ...";
$db_result = $db->fetchAll($query);
$o_select = $o_db->select()
->from("...", $a_columns)
->where('... = ?', ...)
->order('... ASC');
$a_result = $o_db->fetchAll($o_select);
public function fetchAllBy...($...)
{
...
return new ..._Model_...Set(...);
}
try looking at github.com/codeinchaos/restful-zend-framework . You don't have to use it - just look at how it's done.
Blog of the author of this project:
www.codeinchaos.com/post/3107629294/restful-services-with-zend-framework-part-1
www.codeinchaos.com/post/3645298058/restful-services-with-zend-framework-part- 2
I create 2 files: application/models/DbTable/Items.php
class Application_Model_DbTable_Items extends Zend_Db_Table_Abstract
{
protected $_rowClass = 'Application_Model_Item';
protected $_name = 'items'; //Table name
}
application/models/Item.php
class Application_Model_Item extends Zend_Db_Table_Row_Abstract
{
public function getName(){
return $this->title; //database table column
}
}
Then in the controller:
$itemsTable = new Application_Model_DbTable_Items();
$items = $itemsTable->fetchAll();
foreach($items as $item){
echo $item->getName() . '-';
}
The advantage of this method in the model is that you can write additional functions. DbTable return Application_Model_Item object
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question