开发者

new controller in oo php

I have a practical doubt ,

I am using OO php , and i am new to it. I am using zend framework to be clear.

I am writing con开发者_如何学编程trollers(class) and actions(methods) with in it say PatientMapper.php which has all single mysql table related actions and Patient.php which has all setter and getter functions.

I get a doubt that when should i write a new controller.

  1. Should i write a controller for all the actions on a single mysql table .

  2. or a single controller for all actions related to a module.


As opposed to the previous answers, I would say that your controller should not be related to your DB.

Controllers don't handle business logic, your models do.

Besides that, you can write a controller for each entities.

User is an entity, which can be wrapped in a controller, even if it depends on several tables. If your controller is getting bigger and bigger, you can switch to module (Zend Framework terminology) and create an User module, which has an Account controller, etc...


I think you should write controller for single mysql table, because if your application will grow up you can end with few thousand line controllers.


A controller groups actions that conceptually belong together somehow. The controller might use one specific model class (which is not necessarily a database accessing class) only, but it may also use many of them.

Important is, that the controller should not contain the logic of the model classes. The sole responsibility of a controller is to receive and delegate the input for a specific interaction you allow users to do with your application to the model. And in webbased MVC, it is usually also responsible to update the View according to the result of the operation.

The most important distinction one has to understand in MVC is that M should be able to live on it's own while V and C belong together. V and C form just the UI to your application and they should not do anything beside that. Basically, your M is your application, while your VC just sits on top of it. Should really be M|VC imho

With that said, if you feel your application can get away with a single Controller, then use a single appController. But once you find yourself adding actions that could conceptually fit into a separate controller, then add a new controller. If your application is a simple CRUD app, then have one controller per database table. If it does something else, then give it something that fits the scenario.


IMHO I would suggest a action for each mysql table related action for maintainability. These would be ligtweight actions that just call your model for that table


@gordon - Yeah my application is a CRUD application. SO i need to create a model for each table..This is the most accepted way rite. And yeah i am bit clear abt controllers..

i have a page where in i need data from 3 tables. So i will need to call all 3 model methods to get the data rite...

I have a small doubt...i use code like this at the beginning of the beginning of class .and this was written by other developer....i am using it.. i am finding it difficult to use joins ,bcos this is locking the DB..any help on how to use joins over here

 protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Appointment');
        }
        return $this->_dbTable;
    }

this is the select part..not able to use join

  $select = $this->getDbTable()->select()
                        ->from("patient_appointment",array('pa_pd_patient_id',
                                'DATE_FORMAT(pa_datetime,"%h:%i %p") as pa_time','pa_datetime','pa_um_id'
                                ,'pa_created_date','pa_mode','pa_type'))
                        ->where('date(pa_datetime) = ?', $date)
                        ->where('pa_pd_patient_id = ?', $patientId)
                        ->order('time(pa_datetime)');
        $resultSet = $this->getDbTable()->fetchAll($select);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜