Welcome Guest, Not a member yet? Register   Sign In
TRUE contoller CRUD? Another way to not repeat myself?
#11

[eluser]Devon Lambert[/eluser]
[quote author="Mareshal" date="1268861473"]Devon, You can't reduce it too much. It's like you would want to buy a brand new car, with $10 and you still want it cheaper[/quote]

Hmmmm, I don't know Mareshal. I mean that must be one beat up car if it only drives in circles. :-)

Don't get me wrong, I love the way the code is flowing thus far, but I can blatantly see myself writing the same lines of code over and over again.

I've already thought around how to reduce the view code, but the controller code is what gets me. Surprised that this one isn't an easier fix is all. Which in now way means I dislike CI. I love CI more than any other framework out there.

Code:
$this->load->model('model_for_this_controller');

function add ('this_controller'){
  //reusable code to add my controller's item (i.e. page, site, theme)
  $this->model_for_this_controller->add($data);
  $this->load->view('success_page');
}

function edit ('this_controller'){
  //reusable code to edit my controller's item (i.e. page, site, theme)
  $this->model_for_this_controller->edit($data);
  $this->load->view('success_page');
}


function delete ('this_controller'){
  //reusable code to delete my controller's item (i.e. page, site, theme)
  $this->model_for_this_controller->delete($data);
  $this->load->view('success_page');
}
#12

[eluser]Mareshal[/eluser]
I had and still have same issue, writing some code over and over, and I am very interested on this topic. You really make me think to an option, especially when submitting a form and validate data...I hate that part
#13

[eluser]Devon Lambert[/eluser]
[quote author="Mareshal" date="1268862552"]I had and still have same issue, writing some code over and over, and I am very interested on this topic. You really make me think to an option, especially when submitting a form and validate data...I hate that part[/quote]

Hey Mareshal,

Have a look at the form generation library from Frank Michel

Should help you some what with Form validation etc...?
#14

[eluser]umefarooq[/eluser]
Hi thanks, you have to write some code what ever, if you can see form generation library, it self you have learn it how its working another learning process, and its add some unnecessary html code also which you don't need, earlier i was also facing these kind to problem form and validation code to write again and again above code is reducing some of your work, to make it more common you can put reusable code in one library which can be access for every controller, example

Code:
// set rules code put in  library let see util
foreach($this->cols as $key => $v){
      $this->form_validation->set_rules($key,$key,$v['rule']);
     }

class util{

public $ci;

function util(){
   $this->ci = & get_instance();
}
  
  set_rules($cols){
       foreach($cols as $key => v){
           $this->ci->form_validation->set_ruels($key,$key,$v['rule'])
    }
}
create_form(){}
}

another thing if you are using any IDE like Netbeans giving good feature of code template or you class file template example here is one admin controller Netbeans template if you module name is like your controller you can call it also

Code:
class ${name}_admin extends Admin_Controller {
    //put your code here
    function ${name}_admin(){
        parent::Admin_Controller();
        $this->load->module('${name}_m');
    }

        function _remap($task){
        switch ($task){
            case 'edit':
            case 'add':
                $this->form();
            break;
            case 'publish':
                $this->publish();
            break;
            case 'delete':
                $this->delete();
            break;
            default:
               $this->view();
            break;
        }

    }



    function form(){

    }

    function view(){

    }

    function save(){

        redirect('admin/${name}');
    }

    function publish($publish=0){

        redirect('admin/${name}');
    }

    function delete(){

        redirect('admin/${name}');
    }
#15

[eluser]Devon Lambert[/eluser]
Thanks again umefarooq,

Are you saying that we should steer away from the MVC pattern and call the model functions from within the controller?

I'm specifically referring to this line:

Code:
$row = $this->db->get_where('yourtable',array('id'=>$id))->row();
#16

[eluser]umefarooq[/eluser]
this line of code i put for example, we have to follow MVC pattern, it will keep you code clean, this what beauty of MVC pattern, if have seen people using model with to format their post data, it better to format your data in controller what ever you getting give it to model deal with database, you logic should be in controller and also same thing with view, use conditions in views and models when it requires

another thing i just have look on form generation library that can be also good for generating forms but thing is if you want out put of form according to your layout, if you play with it that's really great thing




Theme © iAndrew 2016 - Forum software by © MyBB