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

[eluser]louisl[/eluser]
Is there a better way to set these class properties after a query?, I'm sure I saw a shorter way somewhere?
Code:
...
$query = $this->db->get();
        
if ($query->num_rows() == 1) {

$row = $query->row();

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

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

}
#2

[eluser]InsiteFX[/eluser]
Code:
return $query->row();
#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;

}

}
#4

[eluser]johnpeace[/eluser]
that's a lot of code.

Sure you couldn't just have:

Code:
$this->data = $this->db->get($table)->result();
#5

[eluser]meigwilym[/eluser]
Do you really need to put the data in properties?

If you tried what jognpeace suggests, $this->data would give you something like

Code:
array(4) {
  ["ArticleID"]=>
  int(1)
  ["SEOLink"]=>
  string(8) "nice-url"
  ["PageTitle"]=>
  string(14) "The Page title"
  ["Title"]=>
  string(9) "The Title"
}

Explain your need to use properties, otherwise use the suggested.

Mei
#6

[eluser]louisl[/eluser]
[quote author="meigwilym" date="1328110391"]
Do you really need to put the data in properties?

Explain your need to use properties, otherwise use the suggested.
[/quote]

A. Typically I pre-populate form data using class properties and also use those properties for insert/update and for the view. If it's all in one model class as defined properties I find it's easy to read and debug any issues.

Thanks for the suggestions so far. The reason I asked the initial question is because sometimes I feel I am writing too much code.




Theme © iAndrew 2016 - Forum software by © MyBB