Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP return self as array of objects from within the class?
    primarykey
    data
    text
    <p>I'm using CodeIgniter to build a php web application, and I'm trying to use good OO practices - of which there appears to be many schools of thought. I specifically have a class biography_model to interact with a MySQL table. This data model has some class properties representing the columns in the table, but it also has some properties <strong>not</strong> in the table such as <code>$image_url</code>. The class constructor function accepts an optional record ID parameter which then fetches that record from the table and sets all object properties by calling the <code>get_biography()</code> method, including the <code>$image_url</code> property not in the table. This way I can instantiate a new biography_model object in the controller with all useful properties ready to go: <code>$bio = new biography_model($id);</code></p> <p>But, what is the best approach when we are returning a multi-row result set of records from the table? For each record I need to also set the <code>$image_url</code>. I could do this in the controller, by querying the list of records in the table and then passing each id into the new biography_model($id) object. But then I would have a situation where the controller is directly querying the database bypassing the model. </p> <p>Instead, I choose to return an array of biography_model objects from within the biography_model.</p> <p>Example: <pre><code> class Biography_model extends Model { /** * This model manages biography information in the 'biography_content' table. * If a biography ID is passed in when instantiating a new object, * then all class properties are set. */ protected $id; protected $person_name; protected $title; protected $image_file_name; protected $image_url; protected $biography_text; protected $active; /** * Constructor * * If an id is supplied when instantiating a new object, then * all class variables are set for the record. */ public function __construct($person_id = NULL) { parent::Model(); if(isset($person_id)) { $this-&gt;set_property('id',$person_id); $this-&gt;get_biography(); } } /** * Sets supplied property with supplied value. */ public function set_property($property, $value) { // Set image path if $value is the file name if($property == 'image_file_name') { $this-&gt;set_property('image_url',$this-&gt;get_bio_img_url($value)); } $this-&gt;$property = $value; } /** * Gets requested property value. */ public function get_property($property) { return $this-&gt;$property; } /** * Returns the biography thumbnail image URL */ public function get_bio_img_url($image_name) { return $this-&gt;config-&gt;item('parent_url').'assets/img/biography/'.$image_name; } /** * Get one or more biography entries */ public function get_biography() { // If the ID is set then set model properties. if($this-&gt;get_property('id')) { $this-&gt;db-&gt;where('id',$this-&gt;get_property('id')); $query = $this-&gt;db-&gt;get('biography_content'); if($query-&gt;num_rows() == 1) { foreach($query-&gt;row() as $key =&gt; $value) { $this-&gt;set_property($key, $value); } } } // Otherwise return result set of all biographies else { // Get the list of record ID's $this-&gt;db-&gt;select('id'); $query = $this-&gt;db-&gt;get('biography_content'); if ($query-&gt;num_rows() &gt; 0) { // New array to return result set $biography_list = array(); // For each record, return a new biography_model object foreach($query-&gt;result() as $value) { $biography_list[] = new biography_model($value-&gt;id); } } return $biography_list; } } } // End of Biography_model Class </code></pre> <p>It works. But is it a reasonable approach? Are there other more accepted methods? I'm keenly aware that I am querying the database twice, but I was not sure of a better way to handle this. All suggestions are welcome!</p> <p>Thanks, Wolf</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload