zepernick / Codeigniter-DataTables

CodeIgniter Library For Ajax Server Side DataTables 1.10 >
MIT License
93 stars 94 forks source link

DatatableModel implemented class cannot extend CI_model #2

Closed gfiore88 closed 9 years ago

gfiore88 commented 9 years ago

If I extend CI_model and implement DatatableModel all the methods that are not directly linked with datatableModel don't work.

In short, loading page goes in escape with a 500 error.

If i take out the implements clause the page suddenly works. Why is that?

zepernick commented 9 years ago

My guess is that you don't have all of the methods from the DatatableModel implemented. You have to provide a default implementation even if you are not using the method.

At a minimum, the only method that cannot return a NULL is fromTableStr(). This is what a very basic implementation would look like

    class store_dt extends CI_Model implements DatatableModel{

        /**
         * @ return
         *      Expressions / Columns to append to the select created by the Datatable library
         */
        public function appendToSelectStr() {
                return NULL;
        }

        public function fromTableStr() {
            return 'MyTable';
        }

        /**
         * @return
         *     Associative array of joins.  Return NULL or empty array  when not joining
         */
        public function joinArray(){
            return NULL;
        }

    /**
     * 
     *@return
     *  Static where clause to be appended to all search queries.  Return NULL or empty array
     * when not filtering by additional criteria
     */
        public function whereClauseArray(){
            return NULL;
        }
   }

Let me know if that solves the problem.

zepernick commented 9 years ago

Have not heard back in over 2 weeks and this was not a bug.

servogod85 commented 9 years ago

Actually We fixed the problem. The interface was not being loaded for some strange reason. That is, if you use both extend CI_Model and implements Datablae, for some strange reason, the datable inteface will not be loaded. If you put the following code befora declaring:

get_instance()->load->iface('Datatable');

Then in MY_Loader you need to implement iface:

    public function iface($name){
        require_once APPPATH.'libraries/'.$name.'.php';
    }

Then bothe the extends and the implements will finally works. Cheers,

Salvo and Giovanni

zepernick commented 9 years ago

Hi Salvo and Giovanni,

Thank you for getting back. I would like to look into this further, but I have not seen this same issue occur. Are yoy using HMVC?

servogod85 commented 9 years ago

Yes we are, Our controllers extend a MY_Core controller which then extends the CI_CONTROLLER, but our models extend directly CI_Model. The problems lies in the fact that this model has to extend CI_Model and implement Datatable. Now, some controllers use the regular methods, while other controllers access the same model through the Datatable interface. By extending the interface, the regular methods that have nothing do with the datatable methods do not work anymore. It is very strange indeed.

servogod85 commented 9 years ago

Here is an example code:


get_instance()->load->iface('Datatable');

class Gara_model extends CI_Model implements DatatableModel {

    public function elimina_gara($id_gara) {
       ....
    }

    public function elimina_elabora_gara($id) {
        ...
    }

    public function getGareElaborabili() {
    ....
    }

//*******************************************************************************************************

    public function appendToSelectStr() {
        return array(
            ...
        );
    }

    public function fromTableStr() {
        return 'elabora_gara e';
    }

    public function joinArray() {
        return array(
            ....
            );
    }

    public function whereClauseArray() {

        return null;
    }

    /*I also implemented this for personal use*/
    public function likeClauseArray() {
        $params = array(
          ....
        );

        return $params;
    }

}

All the methods above the asterisks will not work if the interface is not loaded.

boopathit commented 8 years ago

Hi... you said...

Then in MY_Loader you need to implement iface: public function iface($name){ require_once APPPATH.'libraries/'.$name.'.php'; }

Where in this MY_Loader....? I'm a beginner to Codeigniter. so please help?

Where to put the above code?