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.

No comments:

Post a Comment