Welcome Guest, Not a member yet? Register   Sign In
What is the proper way to handle database data in a model
#1

[eluser]MyDarkPassenger[/eluser]
I was wondering what the proper way would be to get info from a database. I understand I should use Active Record but what I mean is if I wanted to retrieve/edit a record which way is more CI oriented. I included two classes that do the same thing and work with the same data. One is designed like ORM and the other doesn't keep state at all. Which is better?

Code:
/*
version a, These are quick throw togethers for examples sake.

This method uses the ORM like design where the class is passed an id which it to
modify/read the record like it was an object.  Set/Get methods handle operations
and give you the ability to filter the data in them.  This method does require
you to call the record from the view to get the data.
*/

class User extends Model {
    var $id, $name, $pass;

    function User() {
        $this->id = false;
        $this->name = "";
        $this->pass = "";
    }

    function load($id = false) {
        if ($id <> false) {
            $this->db->from('users');
            $this->db->where('id', $id);
            $results = $this->db->get();

            foreach($results->result() as $r) {
                $this->id = $r->id;
                $this->name = $r->name;
                $this->pass = $r->pass;
            }
        }
    }


    function save() {
        $data = array(
            'id' => $this->id,
            'name' => $this->name,
            'pass' => $this->pass
        );

        if (!$this->id) {
            $this->db->insert('users', $data);
        } else {
            $this->db->where('id', $this->id);
            $this->db->update('users', $data);
        }
    }

    function getPics() {
        $pics = get_filenames('./media/pics/user' . $this->id);
        $results = array();
        foreach($pics as $pic) {
            if (preg_match("/_thumb/", $pic, $matches) == false) {
                $results[] = $pic;
            }
        }
        return $results;
    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getPass() {
        return $this->pass;
    }

    public function setPass($pass) {
        $this->pass = $pass;
    }

Code:
/*
version b
This method doesn't keep state and instead is just a grouping of like functions.  It
has the benefit of less dependence between functions and returning an array so database
functionality is only necessary for the controller, however it also requires you to
pass data back so that the function knows which records to deal with.
*/

class User extends Model {

    function load($id = false) {
        $data = array();
        if ($id <> false) {
            $this->db->from('users');
            $this->db->where('id', $id);
            $results = $this->db->get();

            foreach($results->result() as $r) {
                $data['id'] = $r->id;
                $data['name'] = $r->name;
                $data['pass'] = $r->pass;
            }
        } else {
            $data['id'] = false;
            $data['name'] = "";
            $data['pass'] = "";
        }
        return $data;
    }

    function save($data) {
        if (!$data['id']) {
            $this->db->insert('users', $data);
        } else {
            $this->db->where('id', $data['id']);
            $this->db->update('users', $data);
        }
    }

    function getPics($id) {
        $pics = get_filenames('./media/pics/user' . $id);
        $results = array();
        foreach($pics as $pic) {
            if (preg_match("/_thumb/", $pic, $matches) == false) {
                $results[] = $pic;
            }
        }
        return $results;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB