Welcome Guest, Not a member yet? Register   Sign In
Is there a better way to set these class vars after a query?
#3

[eluser]louisl[/eluser]
I think you misunderstand my goal, I'm populating the class Properties, here's a better example, is there any way I can improve getArticle()?

Code:
class articles_model extends CI_Model {

// Database fields
public $ArticleID;   // int
public $SEOLink;  // str
public $PageTitle;  // str
public $Title;   // str
public $Headline;  // str
public $Body;   // str
public $PublishDate; // date
public $Active;   // bool
public $ImageFile;  // str
public $ImageTitle;  // str
public $ImageAlt;  // str
public $CreationDate; // date
public $ModifiedDate; // date

public function __construct() {

  parent::__construct();

}

public function getArticle($ArticleID) {

  $this->db->select('*');
  $this->db->from('Articles');
  $this->db->where('ArticleID', $ArticleID);

  $query = $this->db->get();

  if ($query->num_rows() == 1) {

   $row = $query->row();

   foreach ($row as $key => $val) {

    $this->{$key} = $val;

   }

  }

  return $query;

}

public function old_getArticle($ArticleID) {

  $this->db->select('*');
  $this->db->from('Articles');
  $this->db->where('ArticleID', $ArticleID);

  $query = $this->db->get();

  if ($query->num_rows() == 1) {

   $row = $query->row();

   $this->ArticleID =   $row->ArticleID;
   $this->SEOLink =   $row->SEOLink;
   $this->PageTitle =   $row->PageTitle;
   $this->Title =    $row->Title;
   $this->Headline =   $row->Headline;
   $this->Body =    $row->Body;
   $this->PublishDate =  $row->PublishDate;
   $this->Active =   $row->Active;
   $this->ImageFile =   $row->ImageFile;
   $this->ImageTitle =  $row->ImageTitle;
   $this->ImageAlt =   $row->ImageAlt;
   $this->CreationDate =  $row->CreationDate;
   $this->ModifiedDate =  $row->ModifiedDate;

  }

  return $query;

}

}


Messages In This Thread
Is there a better way to set these class vars after a query? - by El Forum - 02-01-2012, 06:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB