Categories
PHP

A beginner’s look into the Orchid Models

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:

To find a specific entry, use the find() method of the model object. Example:

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:

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:

I have an work around it. Just cast the resultset into an object using this following code:

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

Now you are done 🙂 You can now use the “users” model as following:

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.

2 replies on “A beginner’s look into the Orchid Models”

thanks masnun for writing it up. i’ve written the base activemodel library after using the “active record” libraries in code igniter. so you may find great similarities.

and the DAL, Data Access Layer has almost everything you need to start working on DB. prothom alo blog is based fully on orchid and it runs pretty well :D, surviving a huge BW and good amount of traffic

i will write up some articles on orchid. 🙂 stay tuned.

and thanks again for writing it up. the one you overridden was pretty cool 😀

like it 😀

Comments are closed.