Actions

API15

JTable::bind Example001

From Joomla! Documentation

Revision as of 17:40, 11 November 2008 by Batch1211 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


Contents

Syntax

boolean bind ($from, [$ignore = array()])

Parameters

Argument Data type Description Default
$from array or object An associative array or object which keys/properties are supposed to be bound to the Table Object.
$ignore array or string An array or space separated list of fields not to bind

Returns

TRUE if bind was successfull FALSE if $from is neither an array nor an object and thus cannot be bound to the Table Object

Description

JTable::bind() - This method takes an array or object and binds its values (respectively properties) to the table object. The keys of the $from array, respectively the property names of the $from object have to be the same as the names of the table properties.

Preconditions

JTable is an abstract class. You need to write a child class, to use its functionality. See Part 4 of the MVC Tutorial

Example

Let's say we have a table, that stores greetings of different languages. The corresponding Table Class for such a database table could look like this:

class TableGreeting extends JTable {
 
   var $id       = null;
   var $greeting = null;
   var $language = null;
 
   function __construct( &$db )
   {
        parent::__construct( '#__greeting', 'id', $db );
   }
}

We can now create an Table Object from within a model and bind an array (or object):

//Let's create the array:
$from = array( 'id'        => 6,
               'greeting'  => 'Nǐ hǎo',
               'language'  => 'Chinese',
               'continent' => 'Asia'
             );
//Let's specify which columns shall be ignored. This can be a string or array:
$ignore = 'id';
 
// Let's get the Table Object...
$table = $this->getTable('greeting');
// ...and bind the array to it.
$table->bind($from, $ignore);
echo '<pre>';
print_r($table);
echo '</pre>';
 
/* RETURNS:
TableGreeting Object
(
    [id] => 
    [greeting] => Nǐ hǎo
    [language] => Chinese
)
*/

In the Example above we see, that the 'continent' has not been bound to the Table Object, since 'continent' is not a property of the object. Furthermore, we see that 'id' has not been bound to the Table object neither, since 'id' has been on the ignore list.

See also