Thursday, July 1, 2010

Advance joomla component with table connection part 2

After completing Part 1, your administration panel will appear as follows:




This part is used to describe data which is loaded from tables. As you already understand, modules are used to load table information. This part use to describe data loading from tables. You already know, modules used to load table information. In here we use two modules

administrator->components->com_lanka1->models->lanka1.php

Use these four functions:
  • setId()
  • getData()
  • store()
  • delete()

<?php
/**
* Lanka1 default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');


class Lanka1sModelLanka1 extends JModel
{
/**
* Constructor that retrieves the ID from the request
*
* @access public
* @return void
*/
function __construct()
{
parent::__construct();

$array = JRequest::getVar('cid', 0, '', 'array');
$this->setId((int)$array[0]);
}

/**
* Method to set the hello identifier
*
* @access public
* @param int Hello identifier
* @return void
*/
function setId($id)
{
// Set id and wipe data
$this->_id = $id;
$this->_data = null;
}

/**
* Method to get a hello
* @return object with data
*/
function &getData()
{


// Load the data
if (empty( $this->_data )) {
$query = ' SELECT * FROM #__contactus '.
' WHERE id = '.$this->_id;
$this->_db->setQuery( $query );
$this->_data = $this->_db->loadObject();
}
if (!$this->_data) {
$this->_data = new stdClass();
$this->_data->id = 0;
$this->_data->your_name = null;
$this->_data->your_email = null;
$this->_data->your_tp = null;


}
return $this->_data;
}

/**
* Method to store a record
*
* @access public
* @return boolean True on success
*/
function store()
{



$row =& $this->getTable();

$data = JRequest::get( 'post' );

// Bind the form fields to the hello table
if (!$row->bind($data)) {
$this->setError($this->_db->getErrorMsg());
return false;
}

// Make sure the hello record is valid
if (!$row->check()) {
$this->setError($this->_db->getErrorMsg());
return false;
}

// Store the web link table to the database
if (!$row->store()) {
$this->setError( $row->getErrorMsg() );
return false;
}

return true;
}

/**
* Method to delete record(s)
*
* @access public
* @return boolean True on success
*/
function delete()
{
$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

$row =& $this->getTable();

if (count( $cids )) {
foreach($cids as $cid) {
if (!$row->delete( $cid )) {
$this->setError( $row->getErrorMsg() );
return false;
}
}
}
return true;
}

}






administrator->components->com_lanka1->models->lanka1s.php

Use these two functions:
  • _buildQuery()
  • getData()

<?php
/**
* Lanka1 default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );


class Lanka1sModelLanka1s extends JModel
{

var $_data;


/**
* Returns the query
* @return string The query to be used to retrieve the rows from the database
*/
function _buildQuery()
{
$query = ' SELECT * '
. ' FROM #__contactus '
;

return $query;
}

/**
* Retrieves the hello data
* @return array Array of objects containing the data from the database
*/
function getData()
{
// Lets load the data if it doesn't already exist
if (empty( $this->_data ))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList( $query );
}

return $this->_data;
}
}
Table information is displayed row by row once you add these two files to your component.

next lesson
wAp

No comments:

Post a Comment