Welcome Guest, Not a member yet? Register   Sign In
Best approach to implement save method of model.
#1

[eluser]Unknown[/eluser]
Hi!

I have started new CI 2.0 project. And I wonder that is the best approach to implement save method of model?

I thing there are at least 4 ways:

1
Code:
class Brand extends CI_Model
{
    public function save($id, $name, $descr, ....)
    {
        $this->db->query("
              UPDATE  brands SET name =?, descr=?, .... WHERE id=?",
              array($name, $descr, $id,....));
    }
}


2
Code:
class Brand extends CI_Model
{
    public function save($id, $data)
    {
        $this->db->query("
              UPDATE  brands SET name =?, descr=?, .... WHERE id=?",
              array($data['name'], $data['descr'], $id,....));
    }
}


3
Code:
class Brand extends CI_Model
{
    public function save()
    {
        $this->db->query("
              UPDATE  brands SET name =?, descr=?, .... WHERE id=?",
              array($_POST['name'], $_POST['descr'], $_POST['id'],....));
    }
}


4
Code:
class Brand extends CI_Model
{
    public $name;
    public $id;
    public $description;    

    public function save()
    {
        $this->db->query("
              UPDATE  brands SET name =?, descr=?, .... WHERE id=?",
              array($this->name, $this->descr, $this->id,....));
    }
}

Thanks for any advices/thoughts!
#2

[eluser]sikko[/eluser]
You should avoid using $this->db->query() since codeigniter's db class has the functions to cover most of the queries.
Code:
class Brand extends CI_Model
{
    //in the constructor
    $this->table = 'brands';

    public function save($brand)
    {
        $this->db->where('id', $brand->id);
        return $this->db->update($this->table, $brand);
    }
}

Fill your object in your controller, not in your model
Code:
$brand = new stdClass();
$brand->id = $id_to_update
$brand->title = $your_pdated_title;
$brand->desc.....

if($this->brand->save($brand))
//success
else
//fail

btw, this forum is not made for code, you should try to move your thread.




Theme © iAndrew 2016 - Forum software by © MyBB