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
39 Responses
Jerry Galino
April 13th, 2008 at 1:09 pm
1found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..
masud
April 14th, 2008 at 6:10 pm
2Nice to have tutorial on Orchid. it helps me a lot to start faster with orchid.
@Hasin vai, can it possible to start some blogging session on Regular Expression? Because it pain us (specially myself) a lot during making a new one.
Hope to get one very soon.
thanks
MDavis
April 25th, 2008 at 10:19 pm
3I am not sure that I can completely understand your comments. Would you be so kind as to expand on your reasoning a little more before I comment.
sell my house fast
May 3rd, 2008 at 10:20 pm
4sell my house fast…
… may have contributed to the rise in U.S. home prices. In my judgment…
Current Auto Loan Rates
May 9th, 2008 at 12:40 am
5You got master mind on rchid, that’s why you could able to write a article like this, hats off mate - keep up the good work.
Daniel
May 11th, 2008 at 6:54 am
6I couldn’t understand some parts of this article part 2: working with models by Orchid, but I guess I just need to check some more resources regarding this, because it sounds interesting.
Home Contents Insurance Uk.html
May 11th, 2008 at 12:21 pm
7Hi - just wanted to say good design and blog -
Marco Feindler
May 11th, 2008 at 6:02 pm
8Not a bad idea, but are you sure it will be ok?
Current Auto Loan Rate
May 12th, 2008 at 12:35 pm
9I never thought i will find this much information on rchid today. Nice post mate - keep up the good work.
Debt Consolidation Loans For Bad Credit
May 14th, 2008 at 4:13 pm
10I Googled for something completely different, but found your page about rchid ?and have to say thanks. nice read.
Auto Loan Payment Calculators
May 14th, 2008 at 7:08 pm
11Hello webmaster! I am thoroughly impressed with your knowledge of rchid. Your insights into this article about rchid was well worth the the time to read it. I thank you for posting such awesome information. I find it on Wednesday and very happy reading this…
Us Home Contents Insurance Quote
May 14th, 2008 at 8:27 pm
12Hello! Found your blog on yahoo while searching for Us Home Contents Insurance Quote - quite some good info thanks, J.Sanders
Loan Rate Calculator
May 16th, 2008 at 2:44 am
13Howdy Guru, I fell lucky that I located this post while browsing for loan rate calculator. I am with you on the topic rchid. Ironically, I was just putting a lot of thought into this last Thursday.
Max
May 17th, 2008 at 9:26 pm
14Please keep these excellent posts coming.
Daniel
May 19th, 2008 at 4:06 am
15I read similar article also named rchid, and it was completely different. Personally, I agree with you more, because this article makes a little bit more sense for me
Cheap Home And Auto Insurance 20
May 28th, 2008 at 9:01 pm
16part 2: working with models by Orchid is a quite interesting post but quite difficult to understand for me - L.Dexter
Tárcio Zemel
June 4th, 2008 at 12:55 am
17Hello!
I just enter the blog, knew the project and already read all the articles about the orchid! It seems to be an excellent framework!
I would suggest that provide the framework for an easier way, as in a zipped file, for example.
Another thing: it would be interesting if, every part of the tutorial, a.chm, or something that contains all tutoris, until then, is available? Thus, it is easier for those who do not understand so well when you (like me) to better understand and find information more easily.
Congratulations for the project and, from now on, I follow you on the feed!
http://www.bergel.eu/wiki/?page=c8/13999
June 4th, 2008 at 1:22 am
18cyber sex chat webcams…
MyA9Dt2 | amateur porn webcam video …
http://www.cactus.lt/?page=c8/97720
June 4th, 2008 at 1:47 am
19mature sex cams…
nMbhSt2 | nude webcams web chat …
arman
June 4th, 2008 at 4:21 am
20Yd8unl fdv084y0v4t3cnfv593bv29vb
map
June 9th, 2008 at 6:55 pm
21great work great site thanks http://groups.google.us/group/linkmaps bye see you
Contents Home Insurance Quote Uk
June 13th, 2008 at 4:27 am
22Hello! Found your blog on yahoo while searching for Contents Home Insurance Quote Uk - quite some good info thanks, J.Sanders
Xhbhzxau
June 14th, 2008 at 10:11 am
23nice site dude http://google.us/group/zteens teen =OOO
Fncppghg
June 14th, 2008 at 10:25 am
24good material thanks http://google.us/group/jteens teens getting fucked bjbb
Gtrolyve
June 14th, 2008 at 10:29 am
25good job man http://google.us/group/wteens teen naturalist beaches tssf
Pejmvhge
June 14th, 2008 at 2:56 pm
26good work man http://google.us/group/xteens horny teens fwj
Maclsrtj
June 14th, 2008 at 3:14 pm
27http://google.us/group/kteens sweet teen boys 8(
Edokitgo
June 14th, 2008 at 3:15 pm
28interesting site man http://google.us/group/steens teen naturism vuo
Wupjgchu
June 14th, 2008 at 3:19 pm
29best work man great http://google.us/group/rteens free teen movies 127631
Qpvueczk
June 14th, 2008 at 8:07 pm
30great work man thx http://google.us/group/cteens preteen videos 063751
Dsnopflq
June 14th, 2008 at 8:22 pm
31interesting site man http://google.us/group/dteens hot preteen models 8))
Xrykcvcw
June 14th, 2008 at 8:25 pm
32Very funny pictures http://google.us/group/llteens teenage movie galleries 753
Knnpduxo
June 15th, 2008 at 12:47 am
33hi nice site thx http://google.us/group/bteens teen suicide 497
Bfgcsemj
June 15th, 2008 at 1:10 am
34It’s serious http://google.us/group/qteens free teen video =-DD
Fwotohag
June 15th, 2008 at 1:10 am
35good job man http://google.us/group/yteens preteen model forum 06450
http://siorgs.ucsd.edu/scsc/mugs/?p=j16/79661
June 18th, 2008 at 3:13 am
36cams live mature free…
Enzzwt1 | 100 free webcam sex …
merry
June 23rd, 2008 at 2:00 am
37oPTh9o df1zv853gvrvb7gv94gmlas
www.babesfrom.asia
June 27th, 2008 at 5:12 pm
38slick http://www.babesfrom.asia
bnsasucihiso
June 28th, 2008 at 10:52 pm
39titkahokawure…
exxuconucaxi…
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar
Orchid is proudly powered by WordPress - BloggingPro theme by: Design Disease