working with models in orchid

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


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

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

  • selecting data
  • basic joining
  • insert
  • delete
  • update
  • finding based on criteria

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 = "";

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'");
$comicsmodel->comicname="Nancy & Sluggo";

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 🙂


  1. Do you really think it makes sense to write your own ORM unless you are doing something really different? I would recommend you rather look into Doctrine (yes there was reason to develop Doctrine since its DQL concept is sufficiently different form Propel).

  2. hi Lukas, i was now much aware of doctrine. and i really don’t like propel for it’s configur-o-rama settings. will definitely give doctrine a look. thanks for informing 🙂

Leave a comment

Your email address will not be published. Required fields are marked *