In Orchid you must have the database setup and configured to use a model based on the tables of that database. The cool thing is that, you can use the tables of a database directly without explicitly constructing any models with them. Make sure that you have configured your database connection correctly in “app/config/configs.php”. For each table in the defined database, you can use models like this:
|
$model = $this->model->table_name; |
To find a specific entry, use the find() method of the model object. Example:
|
$result = $model->find("id='10'"); // find the entry with ID 10 |
Remember that the columns are returned as associative arrays or dictionaries. To get the email address of the data marked by ID 10, we use:
|
$email = $result['email']; |
Though I believe that it’d have been better if the resultset was an object as well. Then we would have been able to grab the email address by typing:
|
$email = $result->email // This is not possible yet |
I have an work around it. Just cast the resultset into an object using this following code:
|
$result = (object)$result; $email = $result->email; // Now it works :) |
Well, having an associative array is not bad, but it’s cooler to have an object 🙂
If you are obsessed about OOP, take the advantage of the model class definition and override the find() method to return an object.
Suppose we have a table named “users” which we will convert into a model. Go to the “app/models” directory and save the following file as: usersmodel.php
|
<?php class usersmodel extends activemodel { public function find($p) { $res = parent::find($p); // call the parent class's find() method $res = (object)$res; // cast the array into an object return $res; // return the object } } ?> |
Now you are done 🙂 You can now use the “users” model as following:
|
$model = $this->model->users; $res = $model->find("id='2'"); $email = $res->email; |
Cool, isn’t it ? Orchid is really fantastic when it comes to flexibility. I love it ! 😀
Methods:
Once you have retrieved or constructed a resultset, you can use the following methods to interact with the database:
clean() — Cleans all values of a model’s fields.
delete() — Delete the row that contains the current resultset.
insert() — Once you have added values to each of the fields, use this method to insert the data.
update() — Updates the resultset with altered data.
find() — Finds and returns a single resultset based on the given condition.
findAll() — Finds all the results based on the condition.
findById() — Finds a single resultset based on the “id” field.
save() — Similar to update, it saves the resultset into the database.
join() — For complex queries having “joining” operations.
PS: There are lots more functionalities in Orchid models. Hasin vai taught me many of the fantastic features that the models in orchid have. But unfortunately, I have lost many of those information from my memory. I’ll have to check my chat history or ask Hasin bro again when I need those functionalities. But mainly these techniques described above would do most of my database related tasks.