Welcome Guest, Not a member yet? Register   Sign In
save and edit share same method?
#4

I would propose a different pattern, whether we add or edit is not information that needs to be kept within session. Here the link can tell what is going to happen:

something/add
something/edit/15

Code:
class Something extends CI_Controller {

    protected $edit_mode = true;

    public function __construct() {

        parent::__construct();
    }

    public function add() {

        $this->edit_mode = false;
        return $this->edit();
    }

    public function edit() {

        $id = $this->edit_mode
            ? (int) $this->uri->rsegment(3) // Read the target $id, adjust the rooted segment number, if it is necessary.
            : null;                         // For adding we would not need $id, it is assigned by the server.

        if ($this->edit_mode) {

            // Validate the target $id, check whether the corresponding row exists.
            if (empty($id)) { // This is an oversimplified check.

                // Set an error message (flash).
                // redirect to the index page.
            }
        }

        // Form Validation
            // Insert or update, depending on $this->edit_mode

        $data = array(
            ...
            ...
            'edit_mode' => $this->edit_mode,
        );

        $this->load->view('edit_something', $data);
    }
}
Reply


Messages In This Thread
save and edit share same method? - by iridion2015 - 05-05-2016, 08:40 PM
RE: save and edit share same method? - by ivantcholakov - 05-06-2016, 11:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB