DataMapper


Query

This method functions in the same way as CodeIgniter's Query method except that the object is populated with the returned results.

Use this method at your own risk as it will only be as reliable as your query. I highly recommend using the binding approach so your data is automatically escaped.

The Query method will populate the object with the results so it is very important to remember that you should be querying for data from the objects table. For example:

// Create user object
$u = new User();

// SQL query on users table
$sql = "SELECT * FROM `users` WHERE `username` = 'Fred Smith' AND `status` = 'active'";

// Run query to populate user object with the results
$u->query($sql);

Obviously you wouldn't use the Query method for the above situation since DataMapper's Get method would be more appropriate.

As I mentioned before, it is recommended you use bindings when using the Query method. For example, doing the same as above but with bindings:

// Create user object
$u = new User();

// SQL query on users table
$sql = "SELECT * FROM `users` WHERE `username` = ? AND `status` = ?";

// Binding values
$binds = array('Fred Smith', 'active');

// Run query to populate user object with the results
$u->query($sql, $binds);

The question marks in the query are automatically replaced with the values in the array in the second parameter of the Query method.