Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter models and properties
    text
    copied!<p>I have a question relating to properties for a specific instance of a CI model. For example:</p> <p>There is a model called project_model. In the model it has a method calle Get_Projects:</p> <pre><code>$total_projects = $this-&gt;project_model-&gt;Get_Projects($options); </code></pre> <p>When this is called it creates a property in the model like so:</p> <pre><code>$query = $this-&gt;db-&gt;get('projects');//query $this-&gt;num_rows = $query-&gt;num_rows(); return $query-&gt;result(); </code></pre> <p>So after the method has been called and in the controller, I need to access num_rows:</p> <pre><code>$num_rows = $total_projects-&gt;num_rows; </code></pre> <p>(I know some of you may question the reason behind using num rows in the controller. It's to do with setting the pagination. There may be better ways of doing it but there is no time in this particular project.)</p> <p>My problem is that this creates a syntax error: </p> <pre><code>Severity: Notice Message: Trying to get property of non-object Filename: controllers/projects.php Line Number: 110 ($num_rows = $total_projects-&gt;num_rows;) </code></pre> <p>Firstly why is this? I was thinking of using this: $this->project_model::num_rows instead? But then the num_rows won't be specific to the $total_rows object will it? So it will just be for the entire model.</p> <p>BTW: I read the CI guide on models but there wasn't any information on creating instances of models at all.</p> <p>EDITED: I need the result of num_rows property to be object-specific. So for example: </p> <pre><code>$a=$this-&gt;project_model-&gt;Get_Projects($options); $b=$this-&gt;project_model-&gt;Get_Projects($options); $num_rows = $this-&gt;project_model-&gt;num_rows; </code></pre> <p>The final line will get the result of $b num_rows and not $a. So How do I call it so that I can make it object-specific? (Obviously I could store it before the second call in a variable.)</p>
 

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