Thursday, January 27, 2011

Populate DropDown from the values in Database with Active Records

This is something which gives you a kick start with using ActiveRecords and interacting with the database in PRADO. Active Record gives you a whole lot of stuff that you can do without writing huge chunks of code. So, if you done somthing similar in databases with regular PHP, you would have done something like
  1. Creating a connection object using mysql_connect()
  2. Selecting the database using mysql_select_db();
  3. Executing the query - mysql_query()
  4. Processing the results - mysql_fetch_array() or mysql_fetch_assoc()
  5. Preparing the HTML output (

Well, my PHP basics are still pretty strong. Okay...enough is enough. You need to be smart because everyone does that.

So in my Home.page file (this could be any of your .page file), I first insert the drop down component - TDropDownList

<com:TForm>
<com:TDropDownList ID="cmbTest" />
</com:TForm>
Now in your Home.php file, write a method which fetches all the values from a table.
protected function getTestList()
{
// Returns an array containing all the records from the table
}
Now, in the above method you will write the logic to fetch the data which needs to be fetched from the database. Because, we are doing this with Active Records, I assume that you have already configured your application for the use of Active Records. If not, then go to Configuring the MySQL database for use in PRADO Application with Active Records article and configure it right away.

Okay, so you have configured Active Record for application. Now, we need the ActiveRecord class for the table from which we will pull the data. I assume you already know who to create the Active Record classes using the PRADO shell. If not refer to the article - Creating a Active Record class using PRADO Application Shell

Also, make sure that your directory which stores the Active Record class is included in the in your application.xml. Refer to this article for setting it - Including libraries and custom class files.
Here is the table structure for the test table in the database.

mysql> desc test;
+-----------+--------------+------+-----+---------+----------------+
Field Type Null Key Default Extra
+-----------+--------------+------+-----+---------+----------------+
id int(10) NO PRI NULL auto_increment
test_text varchar(100) YES NULL
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.24 sec)mysql>
Once you have your Active Record class for the above table, write the follwing inside it in our Home.php class. TestRecord is the active record class for the test table above.
<?php
class Home extends TPage
{
protected function getTestList()
{
// Returns an array containing all the records from the table
return TestRecord::finder()->findAll();
}
}
?>
Ok. We need to populate the drop down when the page loads. So, we need to write the onLoad() method in Home.php class. This method is invoked every time the page loads and during callbacks.
<?php
class Home extends TPage
{
protected function getTestList()
{
// Returns an array containing all the records from the table
return TestRecord::finder()->findAll();
}

public function onLoad($param)
{
if (!$this->IsPostBack)
{
// Populate the Test Drop Down from database values
$this->cmbTest->DataTextField = 'test_text';
$this->cmbTest->DataValueField = 'id';
$this->cmbTest->DataSource = $this->TestList;
$this->cmbTest->dataBind();
}
}
}
?>
We are writing populating the contents of the drop down only initially when the page loadas and not when it is posted back for server processing.

So, that's it. If everything works fine, you will have your drop down populated with the values from the database.

Download the working demo of the this tutorial here.

Sunday, January 23, 2011

Configuring the MySQL database for use in PRADO Application with Active Records

If you are creating a web application and not using a database, well that's pretty unlikely. PRADO support majority of open source databases including MySQL, SQLite, PostgreSQL. So you have plenty of options to choose from based on your pocket and interest. My personal favourite has always been MySQL and its used most widely (as of yet, though PostgreSQL inspires me a lot) as well. So, here I am demostrating configuring your MySQL database which you intend to use in your application.

First things first, you need to have a database created for your applications first. Refer to MySQL manual if you don't remember the syntax. I always forget that... ;-) :-D. Also, create a user that has exclusive rights for that database. You would use GRANT command in MySQL. Check out here. Also, if you are lazy enough to write the SQL queries, like one of my friend, use phpMyAdmin.

Okay here are my details.
MySQL Server - localhost (I have my MySQL server installed in my local system only)
MySQL Server Port - 3306 (Default Port)
Database Name - sample_db
Database user - test_user
Database Password - test_user$123

We will configuring the database to be used with Active Record configuration. Active Records helps to simplify the database operations in the code drastically. And PRADO provides a healthy set of libraries for using Active Records efficiently. Please refer here for learning more about Active Records.

Lets start with the configuration steps.

First you need to include the database connection details in your application.xml configuration file like this.



<paths>
<using namespace="System.Data.*">
<using namespace="System.Data.ActiveRecord.*">
</paths>

<modules>
<module id="db" class="TActiveRecordConfig" enablecache="True">
<database connectionstring="mysql:host=localhost;dbname=sample_db"
username="test_user" password="test_user$123" />
</module>
</modules>


TActiveRecordConfig class is the ActiveRecord manager. It manages all the DB calls for all the Active Record classes. Learn more about it here.

Okay. So your configuration is done. Congratulations. What you thought it would be longer...

Go try out your configuration setting with any of your Active Record classes.

Need help with that too ? Okay, how about something which you might use soon or later in your application. Lets populate a drop down from the values in a table from the database. Sounds useful and interesting? I already have another tutorial for the same - Populate DropDown from the values in Database with Active Records. check out...

Or you are in too hurry to get things rolling. Then test your configuration by generating a Active Record class from PRADO application shell. That would test it all.

Happy PRADO...

Saturday, January 22, 2011

Including libraries and custom class files

This is pretty simple stuff. If you have been working in Java, this is exactly same as adding jars and classes to the classpath. Here, just you include them in the application configuration file, i.e. application.xml

Let's assume that you have created all your Active Record files inside App_Code directory inside your protected/ directory. Let's include that in our application.xml

If you open your application.xml, you will see the <paths> tag at the top of the file. This is the section where you will include all files in the App_Code/ folder. Here is how you will do that.

<paths>
   <using namespace="System.Data.*">
   <using namespace="System.Data.ActiveRecord.*">
   <using namespace="Application.App_Code.*">
</paths>

All System related includes in snippet above are related to PRADO framework. All application specific classes are included with Application namespace, for example, Application.App_Code.* in above example.

Happy PRADO...

Creating a Active Record class using PRADO Application Shell

PRADO provides a shell interface to your application to do a lot of stuff using prado-cli.php script. This script is located in framework/ directory of your PRADO installation. I am gonna demonstrate to you how to create Active Record classes for using in your application. Also, it can be used to test if your database configuring in the application.xml are correct or not.

First and foremost, you need to create a directory inside your application where all these classes will reside. A general approach is creating all the Active Record classes in a single directory and then include the entire directory in your application.xml to make them available to use in any page of your application. So, I have created a folder named App_Code/ inside my application directory. Check out the following image.

The table for which we will be creating the Active Record class is shown below. I have already created a sample table test with following structure in my database.
mysql> desc test;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| test_text | varchar(100) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.24 sec)
You need to go to the shell prompt of your application now. Use the following command to do the same. I am executing this command from my document root directory. Refer to the image below for the directory structure.


$ php prado/framework/prado-cli.php shell sample_app


www (I am here while executing the above command)
+
+------prado (main framework directory)
+ +
+ +---- framework
+ +
+ +------ prado-cli.php
+
+------sample_app
+
+--- prototected
+
+------ App_Code
You will have the following interface when you have logged on.

Command line tools for Prado 3.1.7.
** Loaded PRADO appplication in directory "sample_app\protected".
PHP-Shell - Version 0.3.1
(c) 2006, Jan Kneschke

>> use '?' to open the inline help

>>

Lets, generate our ActiveRecord class for the test table. Execute the following command on the prado shell of your application. Make sure you have the App_Code/ folder in your application directory. The script does not create the folder. It already expects it to be there in which it creates the file.

>> generate test Application.App_Code.TestRecord

When the command has successfully executed, you will get the message as

>> generate test Application.App_Code.TestRecord
Writing class TestRecord to file C:\wamp\www\sample_app\protected\App_Code\TestRecord.php

This means your Active Record class is generated successfully and you can start using it in your application.

Happy PRADO till next time ... ;-)

Wednesday, June 03, 2009

Notes...

These all will be written in the PHP file while extending TPage

class Subscription extends TPage
{
public function onLoad( $param )
{
//Here your code will come for dynamic controls
}
}



Getting the ClientManager controller...
$clientScriptManager = $this->getPage()->ClientScript;


Including a javascript file to be included in the Head Section
$clientScriptManager->registerHeadScriptFile("js_file_key","js_file_location")


Including a javascript code to be included in the Head Section
$clientScriptManager->registerHeadScript("block_script_key","block_script_in_string");


Adding a control inside a named TContent control(<com:TContent ID="content"></com:TContent>)
$this->content->add( "your new control" );

For example:
$this->content->add( new TTextBox() );

You can also put static HTML...they also be put in the order
$this->content->add( "<h1>This is a heading coming from the PHP script</h1>" );



Link

Sunday, May 31, 2009

Getting Started

Hi...

Lets get started with the PRADO development.

First thing is the installation. Installation is not at all a problem. Just download the .zip file from the PRADO Website. Extract the contents into the DocumentRoot of your web server. We had a Ubuntu 8.10 system ready for it. It had already installed Apache, PHP, and MySQL. So, the development environment is ready for it.

Thats it...its done. As simple as that. Just launch the browser (Mozilla Firefox...the best browser) and type http://localhost/prado/. The default page for PRADO framework opens. It gives you the overview to the framework, some initial requirements, installation details, included demos, ppl to thanks...they truly deserve it, and some other stuff...

Now, to start up with it...the first thing was the demos. They have everything that you require for development. The only thing is one have to churn out the stuff he wants. Anyways, its not at all difficult.

It also includes a pdf for quick start. It includes the stuff to get you started with it. I had a long time with that pdf in the night. Its the best time for nocturnal creatures like us...he he he. Well, started off with the basic info and then to the first application for most languages...Hello World.

The steps are given very clearly in the quickstart.pdf. So, nothing is complicated. The amazing thing to me was, how less the code was written. Just execute the prado-cli.php to create the directory structure, change the stuff in Hello.page, to be precise, just one line for Hello World. And the application was up. Nothing at all...It was that simple.

Getting to work with php. Just add the stuff in the Home.php for appropriate Event and you are done. The tutorial includes a TTextBox...Prado's implementation for textboxes which changes its text onsubmit of the page. Just write the corresponding event in Home.php....and it was doing the stuff...

So, just the basic thing. Nothing complicated....got started with the framework. Hope the pace continues as we go along...

The Background

Well, completing graduation is very nice. And then everyone looks for something interesting. Here is what dra@g0n is up to. Me, along with my other 3 real special friends are working on a really interesting project.

What we decided was to use some PHP framework. I checked out a lot of frameworks. With a little background of CakePHP and CodeIgniter, I decided to choose PRADO for our requirements. What made me choose PRADO over others was that it had Object-Relational Model, Ajax support, Component based development, and event-driven programming. Event-driven programming is a new addition to PHP frameworks and I found only PRADO possessing that capability.

So I downloaded and hence the journey to a new dimension started...

It will be a really amazing and adventurous journey for sure. Lets see how much can we enjoy.

I am writing these blog, so that anyone who is starting a hands on PRADO feels that.........he is not alone.....we all are there for you buddy....... just keep going...