as promised, orchid comes with real flexibility which lets you design your application in less time. in orchid, there is a nice active record object which helps you to interact with your database very easily. beside that, you can also write your own model class to facilitate comples business logics and operations. in this installment, we will learn how to achieve maximum speed and benefit by using the builtin model library of orchid with a toppings of the active record library.
2.1 connecting to a database
the built in data access libraries in orchid will help you to connect to mysql, postgresql, mssqlserver and sqlite in a minute. beside that you can also connect to other database engines by using native wrapper of pdo. all you have to do is to add a few lines in the configuration file and whoa, you are connected. don’t you believe me? lets see
create a file named configs.php in app/config/configs.php with the following contents
$configs['db']['production']['dbname']="";
$configs['db']['production']['dbhost']="";
$configs['db']['production']['dbuser']="";
$configs['db']['production']['dbpwd']="";
$configs['db']['production']['persistent']="";
$configs['db']['production']['dbtype']="";
in orchid you can have as many database state (such as production, development or whatever) as you want. in the example above, we named our db state as “production”. rest of the code is self explanatory, right? the only confusion you might have is about “persistent”. well, if you set it to true, the connection to your database stay alive as long as you are using the same connection strings, which will save some microseconds from connecting every time your script executes.
you are not done yet. there are some more configuration directives we have to add. we haven’t yet specified the type of our database and which db state orchid will use, lets do that. add the following two lines in your configs.php
$configs['db']['usedb']=""; //true or false
$configs['db']['state']="development";
the value of “usedb” could be either one of “mysql”,”mysqli”,”pgsql”,”mssql”,”pdo” or “sqlite”. and using usedb you can tell orchid to use database engines or not. so you can save the connection parameters for use later by specifying the “usedb” parameter to “false”.
ding dong! you are connected to your favorite db engine in a minute, as said.
2.2 good coding and bad coding while working with database
in orchid (and in most other framework possibly) you can directly use the internal dbmanager to execute sql, fetch the result and manipulate that through out your application. i have seen developers manipulating records in controllers, in views and skipped the most appropriate part where these manipulation should take part in fact, models. many of you dont make use of the models in your application and write the business logic entirely in your controller, feeding the inner monster of your application to be the most vigorous unmanageable giant. dont blame php as a tool to write unmanageable code because this unmanageable code solely depends on you - you. always try to use models to encapsulate the business logic and separate it from your controller. lets model do it’s job.
2.3 working with models
in this section we will write a simple model and insert some data inside a table. here comes the schema of the table.
CREATE TABLE `comics` (
`id` int(11) NOT NULL auto_increment,
`comicname` varchar(255) default NULL,
`comicurl` varchar(255) default NULL,
PRIMARY KEY (`id`)
)
in orchid, you can instantly create a reference to any table in your database by auto model feature of orchid, which looks like the code below, which will reside in a controller.
$comicsmocdel = $this->model->comics;
orchid will dynmically create a reference of the “comics” table instantly, no more hassle for you. but there are significant causes why we will write models by coding it. the auto models support only the very basic data manipulation. if you want to encapsulate business logic inside, we definitely need to write the code inside. lets learn first how to work with a auto model like this one.
a model in orchid supports the following methods
as we have already created a model, lets make use of it. the following piece of code will insert a record in the comics table using the model.
$comicsmodel = $this->model->comics;
$comicsmodel->comicname = "Nancy and Sluggo";
$comicsmodel->comicurl = "http://comics.com/comics/nancy/index.html";
$comicsmodel->insert();
this is really simple to work with models in orchid, like as i said, the above example will insert a data in your data table named “comics”. now lets see how we can modify the existing data. in the following example we will change the name of the comic to the data which we’d just inserted.
//inside the controller action
$comicsmodel = $this->model->comics;
$data = $comicsmodel->find("comicname = 'Nancy and Sluggo'");
print_r($data);
if(!empty($data))
{
$comicsmodel->comicname="Nancy & Sluggo";
$comicsmodel->update();
}
the find method can take any clause and find records according to that. similarly as update and insert, you can delete records using models in orchid.
in upcoming tutorials we will learn more on models and encapsulating business logic by writing in by our own
today i was planning to start the documentation of orchid framework for the rest of us(?). so this one is the first installment of this series. you will find small tutorials everyday. just stick with the series and you will see how far we can fly you. time to get high
[please note that the text in this series will be all-small cap]
getting started with orchid
orchid is a small framework with bare necessities to kick start developing killer php web applications. this framework is not flooded with unnecessary features and libraries. it only contains the essential helpers and libraries to boost up your development, not slowing it down. orchid features a very short learning curve, which will keep you trouble free. small footprint and excellent profile will be your first choice, which you will definitely realize once you start realizing the power that orchid brings in your hand. long story short, orchid is the next framework going to rock.
you can checkout orchid from it’s subversion repository
svn checkout http://orchidframework.googlecode.com/svn/trunk/ orchidframework
orchid comes pre equipped with excellent configuration directives in a config file, which we will focus in details in the upcoming tutorials. for now lets just start as is.
to create your first application, we need to create a folder named “app” inside the orchidframework directory. after you create that, there will be three directories named app, core and tests.
in orchid, controllers reside in app/controllers directory. views (templates) reside in app/views/controller_name/ directory, which means if the name of your controller is “blog”, all your views remains in app/views/blog/ directory. lets create our first controller named “talk”. the controller file will be app/controllers/talk.php.
a controller in orchid is a normal php class file which extends the base controller class. all the public methods in a controller class are considered as action. a method named “base” is the primary action for all the controller in orchid. and for now, this is all the information you need to create your first controller in orchid.
lets write a sample action in our controller, named “base”
<?php
/**
* the source file is app/controllers/talk.php
*/
class talk extends controller
{
public function base()
{
echo "hello";
}
}
?>
now you can run this controller by pointing your browser to the following location. i considered that you have hosted orchid in your document root (i.e /var/www/orchid)
http://localhost/orchid/talk
whoops, what have we done wrong? why do we get an error?
we are seeing this error because we didnt write our view (or template, whatever you call it) yet. and this is not a good coding practice to output anthing directly from your controller. whatever the size of the output, you should display it by passing the data to a view first. this will ensure the consistency of your project through out the application. orchid is flexible, but keeping the word “flexibility with responsibility”
lets write the view file, named “<i>base.php</i>” which will reside in app/views/talk/ directory.
<?//the source file is app/views/talk/base.php?> <?=$talkings;?>
so where from do we get the variable named $talkings? dont worry, you will have to pass it to the view from your controller. in fact through out your application you have to pass all the data to a view from the controller in almost every mvc framework and orchid is not the exception in this case. lets re write the controller
<?php
/**
* the source file is app/controllers/talk.php
*/
class talk extends controller
{
public function base()
{
$this->setViewParam("talkings","hello world");
}
}
?>
now you will see the output “hello world” as output when you point your browser to http://localhost/orchid/talk
orchid is very flexible and if you need to raw output (for example after a ajax request) some content from your controller, you can even do that. orchid wont restrict you from not doing so, but what we really said is that for good coding you should follow this guideline.
so that’s it. getting started in orchid wont take more than 5 minutes and that is where the blessings of using a framework is in. in next tutorial we will learn how to use a model in orchid.
Ok, lets try to create baby out of orchid
Take her at your place for date:
Copy the orchid folder to a location of choice where you like to host the app.
Make sure she is vergin
There should not be any app folder inside the orchid folder. If any, remove it.
Get inside of her, make her move…
Open up a console, go to the orchid directory. run:
$php cli.php skeleton test
You will find a new folder named app is created in orchid directory.
The last drop….
run the following command:
$php cli.php controller main
Bingo.. orchid is a very good mother and wont take 9 months to create a baby. You can see your baby at:
http://localhost/learn/test/orchidframework/index.php?main
Don’t forget to change the url as required for your setup.
Examining the baby:
in orchid/app/controllers folder a new file is created named main.php
and two more files in orchid/app/views/main/ folder naming base.php and hello.php.
What they do is really simple and easy to understand.
Here is the controller file content:
class main extends controller
{
function base()
{
$this->view->set(”name”,’main’);
}function hello()
{
$this->view->set(”param1″,’World’);
}
}
and hello.php and base.php are normal php files.
http://localhost/learn/test/orchidframework/index.php?main
This line executes the base method of the controller named main. ‘Base’ method is called implicitly if no explicit method is asked for.
and http://localhost/learn/test/orchidframework/index.php?main/hello
calls the hello method of the controller main.
Simple, eh ?
Just one more thing, you can pass values to the view from controller. Look at the hello method once again.
$this->view->set(’param1′,’world’);
And here how the param1 is used in hello.php
Hello <?=$param1;?>
Orchid’s baby is really smart, right ?!!
Cheers ![]()
Recently we have added http object, blueprint css framework and RSS reader and writer class in orchid. Due to having some complexity, we decided to add support of open id in next stable release.
The project is running under heavy documentation. You can expect a beta very soon
If you are interested to take part in documentation team, please drop me a mail.
We have added a new object in orchid framework. This is an object to help you manage all remote operations (over HTTP) extremely easier. This is a wrapper of HTTP class that uses either cURL or fsockopen to harvest resources from the web. It supports a handy subset of functionalists of HTTP that are mostly needed in day to day coding. Scripts who need to communicate with other servers will find it useful. If you’re looking to invoke any RESTful API and don’t want to bother adding a bunch of libraries for that simple thing, just put this class and you’re set.
Thanks goes to Emran for contributing this one ![]()
Two domains for orchid is taken today. They are

Not both of them are in same group. OpenID is one of the most successful single sign on solution for open source developers and BP is Blueprint, a really nice CSS framework.
We are planning to bundle both of them in orchid in next release. Needless to say, BP will work as an optional choice for developers who will use Orchid.
Wait for the big time, we are sure that you will find it rock!
Not a big deal in Orchid. If u have a table named “users” in your mysql database with the following structure, you can interact with it like this.
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(250) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM
Here is how to interact using a model
$usersmodel = $this->model->users; $usersmodel->username = "test"; $usersmodel->password = "testpass"; $usersmodel->insert(); $usersmodel->find("username = 'test'"); $usersmodel->username = "tester"; $usersmodel->update();
You dont need to write any code for this model “users”. It is auto generated using the active record library that comes with orchid. You can generate models by naming them same as table names in your database. Easy, huh?
Writing inline unit test makes it painless and hassle free. Orchid comes with a bundled unit test library which you can take full advantage of. Have a look at the following code to understand how to write your unit tests. No more “setup” and “teardown” for you
Lets consider your controller is app/controllers/ut.php
<? class ut extends controller { function base() { $this->use_view=false; $name = "Orchid"; unittest::assertContains("Orchid",$name); $gmap = $this->library->gmap; $location = $gmap->getGeoCode("dhaka"); $longitude = $location['lon']; $lattitude = $location['lat']; unittest::assertEqual($lattitude,"23.709801"); unittest::assertEqual($longitude,"23.709801"); } } ?>
Now point your broswer to http://your_orchid_path/ut/base/unittest. You have to write “unittest” at the end to view the output of test cases from inline unit tests. Otherwise it will work as regular action and no unit test result will be displayed in output. Nice, huh?
Now see what it generates as a result of these assertions
Test passed in Method: base. Line: 8. File: C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\orchid\app\controllers\ut.php.
Expected: Haystack has a needle named ‘Orchid’, Actual: Haystack has a needle named ‘Orchid’.
Caching is one of the built in core features in orchid. Orchid supports three types of storage engine for orchid which are SQLite, MySQL and Memcache. You can take advantage of either one depending on your requirement. Lets have a look at how to work with caching engine in Orchid.
1. Setup configuration file
if you want SQLite as your caching storage, Use the following directives in your app/config/configs.php
$configs['cache_sqlite']['dbname']="sqlite.sq2"; //absolute path or it will be stored in app/cache $configs['cache_source'] = "sqlite"; //or mysql or memcache
If you want MySQL as storage , use the following directives
$configs['cache_mysql']['dbname']=""; $configs['cache_mysql']['dbuser']=""; $configs['cache_mysql']['dbhost']=""; $configs['cache_mysql']['dbpass']=""; $configs['cache_mysql']['dbpersistence']=true; $configs['cache_source'] = "mysql";
You need to create a table named `cache` with following schema
CREATE TABLE `cache` ( `marker` varchar(250) NOT NULL, `content` text, `valid` int(11) DEFAULT NULL, `modified` int(11) DEFAULT NULL, PRIMARY KEY (`marker`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1
If you use Memcache as storage, use the following directives
$configs['cache_memcache']['servers'][]=array("host"=>"","port"=>""); $configs['cache_memcache']['servers'][]=array("host"=>"","port"=>""); ... as many you want $configs['cache_source'] = "memcache";
2. Now take a look at the code of the controller
<? class cachetest extends controller { function base() { $this->use_view=false; $cache = loader::load("cache"); $cache->set("key1","Hello World",2); $cache->set("key2","Hello World2",5); sleep(3); if ($cache->isExpired("key1")) echo "This key has been expired after 2 seconds"; echo $cache->get("key1").":".$cache->get("key2"); } } ?>
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | ||||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |