Welcome Guest, Not a member yet? Register   Sign In
Does my CRUD is good??
#1

[eluser]Unknown[/eluser]
Hello
I'm start playing with php and codeigniter
and i'm wonder if my code is good
So it will by nice if you would tell me if somethink with my code is wrong.
Maybe somethink i should add or delete.



Code:
class Account_M extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    var $account;

    // Get account data from database by id
    function load($param) {
        if ($this->account = $this->_load('id', $param))
            return TRUE;
        return FALSE;
    }

    // Get account data from database by name
    function loadByName($param) {
        if ($this->account = $this->_load('name', $param))
            return TRUE;
        return FALSE;
    }

    // Return specific field declared by $key
    function get($key) {
        if (array_key_exists($key, $this->account))
            return $this->account->$key;
    }

    // Return all account data
    function getAccount() {
        return $this->account;
    }

    // Set account data
    function set($key, $value) {
        if ($key !== 'id')
            $this->account->$key = $value;
    }

    // Create account database and return his id
    function create() {
        if (!isset($this->account->id)) {
            $this->db->from('accounts')->set($this->account)->insert();
            return $this->account->id = $this->db->insert_id();
        }
    }

    // Update account data in database
    function save() {
        if (isset($this->account->id))
            $this->db->set($this->account)->from('accounts')->where('id', $this->account->id)->update()->affected_rows();
    }

    function _load($key, $value) {
        return $this->db->from('accounts')->where($key, $value)->get()->row();
    }

}

CRUD without Delete function XD


Sorry for my bad english.
#2

[eluser]gidarren[/eluser]
I think its good, and I like the fact that you optimized your IF statements.




Theme © iAndrew 2016 - Forum software by © MyBB