Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Do version 1 and cache the results of your db query into fields in your object. You don't want to go to the db every time over the lifespan of the object and in a web app an object's lifetime is only the single page request, so there is little chance of non-atomic data operation pollution.</p> <p>Further, don't connect to the database in your constructor. Take the time to build a class factory to generate your classes. Honestly you can even write a very simple code generator to generate both the model class that stores all your data fields and the mapper class that goes to the db and builds your object from a table in under 100 lines of code.</p> <p>If you have the extra time, learn and use a framework like zend or for just data, doctrine...</p> <p>(FWIW, I think learning simple zend table stuff would net you a time savings)</p> <pre><code>class User{ protected $_id; protected $_email; protected $_username; protected $_userFirstName; public function __construct(){ } public function setId($id){ $this-&gt;_id = $id; return $this; } public function setEmail($email) { $this-&gt;_email = $email; } // etc and getters too } public class UserMapper { protected $_connection; public function __construct($connection){ $this-&gt;_connection = $connection; } public loadUser($id){ $select = $this-&gt;_connection-&gt;select(' * from Users') -&gt;where('id = ?', $id); $result = $select-&gt;fetchAll(); //error checking here $user = new User(); $user-&gt;setId($results[0]['id']) -&gt;setEmail($results[0]['email']) -&gt;setUsername($results[0]['username']); return $user; } public saveUser(User $user){ } } </code></pre>
 

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