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.

Categories
PHP

The Orchid Experience

For our next project at Leevio, it has been decided that we will use Orchid as our PHP framework. It’s a framework created by Hasin Hayder, the founder of Leevio and the PHP Guru of Bangladesh. Today I was assigned to make myself used to this framework. I don’t have any prior experience of working with a fully fledged frameworks. So I was a bit worried. I checked out the Orchid framework codes from the Google Code SVN repo and installed the framework in my localhost. First I failed to make it work because by mistake I have left the .htaccess file which was hidden in my file explorer by default. Soon, I understood that I have left behind the lifeblood of a framework — the URL rewriting definition in the .htaccess file. I quickly pressed Ctrl + H to unhide the hidden files and copied the .htaccess file into my www/orchid directory. Whoa! I was now able to see the welcome page at http://localhost/orchid/. While I must admit that Hasin vai has a worse sense of what a welcome page should look like, on the other hand I must admit that the messy page took much of my tension away 😉 😛

I examined the sample apps for a while but since I have no prior experience in a whole framework like Orchid, I was having a tough time understanding how the wheel rotates inside Orchid. But I could ocassionaly make some educated guesses and my asking Hasin vai proved that all of them were correct 🙂 I was now more comfortable at exploring this new Island that at first seemed to be so full of vicious creatures.

After a while, I realized that I am that sort of stupid who can’t understand theories until he has done some practical work. I decided to do something on my own. While browsing the source, I found a “cli.php” in the root directory of Orchid. Those who know me or have read enough of blogs, they’d know that I am badly fascinated by cli tools. Examining the cli.php file, I could figure out that this one creates skeleton codes for an app. The cli tool has two options — create a skeleton app or an app with pre-installed controller and views. I chose the second option.

First, I removed the samples those came bundled up with Orchid. Then I opened up my terminal and fired off typind the following codes:

That’s all I needed. First one changed the current working directory to the Orchid directory. The second one invoked the Orchid CLI tool to create a controller for “masnun”. Again, I should criticize Hasin Vai, the CLI tool prints nothing but a single line saying — “Welcome to the CLI tool of Orchid” or something like that. Though it gives an error message on failure, I don’t think thats any credit of the tool in question, rather the error message is generated by PHP itself. On the other hand, on success, you get not even a single line congratulating you or at least saying “hi”. I am very much offended by such rudeness 🙁

Anyway, I was now ready to view my app at: http://localhost/orchid/masnun/

It showed a simple text saying “Called from masnun/base method”. That made sense to me. There is a “base” method inside the “masnun” controller. I opened up the controller file and yeah, there really was a base method and another method named “hello” 🙂 I was curious to see, what this other method does. So, I visited out of curiosity the following URL:

http://localhost/orchid/masnun/hello/

The result was somehow obvious, yeah it printed “hello world!”.

Now I came to think about it. How does Orchid work ? Well, I was feeling stupid now, since it has now been quite clear. The URL format is:

/controller/method.

If you just visit the controller, the base method is called. That is:

“/controller/” == “/controller/base/”

It was easy as pie! Then I moved on to using templating. Opened up the view directory and found two files — “base.php” and “hello.php”. I had no doubt that they had markup for each of the method of the controller. I opened up the controller again to examine.

Aha… I don’t even have to explicitly define the view ? Cool… Hasin vai really has some talent then 😉 Every method auto loads the curresponding view 🙂 That cuts down the work of a programmer for sure. But wait! How can Hasin vai be this stupid ? What if I need to setup a different view ? Oh my god! I have to tell him about this.

I was frustrated with Orchid and closed the current working copies and started examining the sample apps. At that time, I realized that Hasin vai was not stupid at all, he’s a genius ! He does have the “setView()” method. I was relieved and feeling sorry for cursing the gentleman for a while. But still, I would defend myself because if Hasin vai had a good documentation, I would have known it long before and didn’t need to curse him at all. So, you see? It’s his fault after all and he does deserve some curses for not having a total documentation. What do you say ? 😉

I immediately used the following code in the hello method:

Wow ! It does work. I have been able to reproduce the base method view just by changing the view of the hello method. I am damn happy.

Later I found out that, there’s a “layouts” folder inside the “views” directory where I can define the global layout for a controller. I created a “masnun.php” in the layouts directory and put these code:

Cool ! The template was applied to the whole controller — that is to both the base and hello method.

I am just loving this framework! Orchid has some cool built in libraries for additional features. Have a look at it’s core features:

1. Support for Caching
a. SQLite Based Caching
b. MySQL Caching
c. Memcached Caching

2. Built in Unit Testing Support
3. Fast and Lightweight
4. Support for MySQL, MySQLi, SQLite, MSSQL and PgSQL using Native DAL and PDO
5. Support for Layout
6. Active Record
7. Easy Application Setup
8. Flexible Configuration Management
9. Builtin Profiling
10. I18N support
11. Session Manager (Native and DB Based)
12. Excellent support for benchmarking and profiling
13. Builtin JSON Encoder and Decoder
14. Google Chart API Support
15. Bundled JS Libraries with Gzip Support
a. Prototype
b. JQuery
c. SWFObject
16. Builtin Library for jQuery Effects and AJAX Support

PS: I am still learning Orchid and hope to keep posted about my progress. I am now concentrating on understanding the models system of Orchid 🙂 A big thanks to Hasin vai for this wondeful framework !

Categories
Uncategorized

Using SVN with GoogleCode Project Hosting

At Leevio, we are going to use SVN or Subversion a lot. For demonstration and playing with the SVN system I chose Google Code Project hosting since it’s free and feature packed. I am a beginner at operating SVN and I had to get a sandbox SVN server to practise what I learn.

I already had the http://masnun.googlecode.com ready for this. So, I started trying out SVN on this account feeling relaxed that no matter what changes I make, no serious harm will be done.

SVN is pretty easy in fact. I read the “SVN Book” and “The Visual Guide to SVN”. None of them helped me grab SVN like the shell command — “svn help”. It was self explanatory and had a list of all available commands.

To start, I had to create a local repository or better said a local mirror of the project. I did the following:

It created a directory named “masnun” inside my linux home directory after I authenticated with my googlecode password.

Now I was ready to add files. So I copied a file named “quotes” into the /home/masnun/masnun (the local mirror) directory and typed in:

I got a text editing environment to write the changelog. After typing in a message, I pressed Ctrl+O to write out and then Ctrl + X to quit the editor.

Now the changes were reflected in the local copies, I had to sync the server. So I used:

And it transmitted all the data to the server 🙂

Now, I made some changes and updated the file using the following command:

Again, wrote the changelog and then :

I liked the simplicity and hated that I spent up much time reading theories telling me why SVN replaced CVS 🙁

Well, now I decided to delete the file:

That was it… Easy !