Jump to content
Robert1995

Model SQL exemplu pentru Codeigniter

Recommended Posts

Model_Constant


<?php
class Model_Constant extends CI_Model {

function __construct()
{
parent::__construct();
}

const LOGOUT_MESSAGE = 'Succesfully Logged Out';
const FLAG_TRUE = 1;
const FLAG_FALSE = 0;
}

Model_User


<?php
class Model_User extends CI_Model {

var $table = 'user';

function __construct()
{
// Call the Model constructor
parent::__construct();
}

function getAll($ids = array())
{
$sql = 'SELECT * FROM '.$this->table.' ';
$sql .= 'WHERE ';
$sql .= 'is_deleted <> '.Model_Constant::FLAG_TRUE.' ';
if(!empty($ids)){
$sql .= 'AND id IN ('.implode(',',$ids).') ';
}

$query = $this->db->query($sql);
return $query->result();
}

function getById($page_id){
$sql = 'SELECT * FROM '.$this->table." WHERE id = $page_id";
$query = $this->db->query($sql);
$result = $query->result();
return isset($result[0]) ? $result[0] : array();
}

function getByEmail($email_address){
$sql = 'SELECT * FROM '.$this->table." WHERE email_address = '$email_address'";
$query = $this->db->query($sql);
$result = $query->result();

return isset($result[0]) ? $result[0] : array();
}

function insertEntry($page)
{
return $this->db->insert($this->table, $page);
}

function updateEntry($page)
{
if(isset($page['id'])){
$this->db->update($this->table, $this, array('id' => $page['id']));
return $page['id'];
}else{
return false;
}
}

}

Feel free sa adaugati ce doriti la post.

Link to comment
Share on other sites

Nu stiu daca ai facut validarea parametrilor înafara modelului, eu de obicei mai arunc câte un (int) în model pe un de ?tiu ca valorile sunt integer sau câte un mysql_real_escape_string pentru sting

ex :

$sql = 'SELECT * FROM '.$this->table." WHERE id = " . (int);

[code]

Link to comment
Share on other sites

Nu stiu daca ai facut validarea parametrilor înafara modelului, eu de obicei mai arunc câte un (int) în model pe un de ?tiu ca valorile sunt integer sau câte un mysql_real_escape_string pentru sting

ex :

$sql = 'SELECT * FROM '.$this->table." WHERE id = " . (int);

[code]

Deobicei la getById le folosesc pentru queries "interne" , rar cand le folosesc inafara fac cu is_numeric sau is_int cum apuc , oricum modelul e scris de mai demult ,am vrut sa-i arat cuiva cum sa inceapa apoi mi-am zis sa-l postez si pe rst poate mai au nevoie si altii.

Edit: eu acum mai nou fac validarile la forms cu CI Validation , inainte le faceam in model aveam o privata

Edited by Robert1995
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...