Actions

API15

Difference between revisions of "JTable::bind Example001"

From Joomla! Documentation

m (Wilsonge moved page JTable/bind to API15:JTable::bind Example001: Move to new page)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
+
In this example the class '''TableGreeting''' corresponds to a table called 'jos_greeting' in the Joomla database.
===Syntax===
+
''boolean''  bind  ($from, [$ignore = array()]) 
+
 
+
=== Parameters ===
+
{| class="wikitable"
+
!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 [[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface#Creating the Table Class | 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:
+
 
<source lang="php">
 
<source lang="php">
class TableGreeting extends JTable {
+
class TableGreeting extends JTable
 
+
{
 
   var $id      = null;
 
   var $id      = null;
 
   var $greeting = null;
 
   var $greeting = null;
 
   var $language = null;
 
   var $language = null;
 +
  var $_code    = null;
  
 
   function __construct( &$db )
 
   function __construct( &$db )
Line 47: Line 15:
 
</source>
 
</source>
  
We can now create an Table Object from within a model and bind an array (or object):
+
Now, in the model, create an array of fields/properties to be updated and bind it to the table object:
 
<source lang="php">
 
<source lang="php">
//Let's create the array:
+
// Create the array of new/amended fields/properties.
 
$from = array( 'id'        => 6,
 
$from = array( 'id'        => 6,
 
               'greeting'  => 'Nǐ hǎo',
 
               'greeting'  => 'Nǐ hǎo',
 
               'language'  => 'Chinese',
 
               'language'  => 'Chinese',
 
               'continent' => 'Asia'
 
               'continent' => 'Asia'
 +
              '_code'    => 'ASC012'
 
             );
 
             );
//Let's specify which columns shall be ignored. This can be a string or array:
+
// Specify which columns are to be ignored. This can be a string or an array.
 
$ignore = 'id';
 
$ignore = 'id';
  
// Let's get the Table Object...
+
// Get the table object from the model.
$table = $this->getTable('greeting');
+
$table = $this->getTable( 'greeting' );
// ...and bind the array to it.
+
 
$table->bind($from, $ignore);
+
// Bind the array to the table object.
 +
$table->bind( $from, $ignore );
 +
 
 +
// Show the result of the bind for illustration purposes only.
 
echo '<pre>';
 
echo '<pre>';
print_r($table);
+
print_r( $table );
 
echo '</pre>';
 
echo '</pre>';
 
+
</source>
/* RETURNS:
+
This would result in the following output:
 +
<source lang="text">
 
TableGreeting Object
 
TableGreeting Object
 
(
 
(
Line 72: Line 45:
 
     [greeting] => Nǐ hǎo
 
     [greeting] => Nǐ hǎo
 
     [language] => Chinese
 
     [language] => Chinese
 +
    [_code] =>
 
)
 
)
*/
 
 
</source>
 
</source>
 +
Notice that
 +
* the 'continent' array entry has not been bound to the table object, since 'continent' is not a property of the object.
 +
* the 'id' array entry has not been bound to the table object either, since 'id' was on the ignore list.
 +
* the '_code' array entry has not been bound to the table because it is a private property of the table object (its name begins with an underscore).
  
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 either, since 'id' has been on the ignore list.
+
<noinclude>[[Category:CodeExample]][[Category:MethodExample]][[Category:JTable]][[Category:bind]]</noinclude>
 
+
===See also===
+
* [[JTable/load]]
+
* [[JTable/store]]
+
* [[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface#Creating the Table Class | Creating a JTable Child Class (MVC Tutorial Part 4)]]
+
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JTable]]</noinclude>
+

Latest revision as of 03:00, 3 July 2013

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.

In this example the class TableGreeting corresponds to a table called 'jos_greeting' in the Joomla database.

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

Now, in the model, create an array of fields/properties to be updated and bind it to the table object:

// Create the array of new/amended fields/properties.
$from = array( 'id'        => 6,
               'greeting'  => 'Nǐ hǎo',
               'language'  => 'Chinese',
               'continent' => 'Asia'
               '_code'     => 'ASC012'
             );
// Specify which columns are to be ignored. This can be a string or an array.
$ignore = 'id';
 
// Get the table object from the model.
$table = $this->getTable( 'greeting' );
 
// Bind the array to the table object.
$table->bind( $from, $ignore );
 
// Show the result of the bind for illustration purposes only.
echo '<pre>';
print_r( $table );
echo '</pre>';

This would result in the following output:

TableGreeting Object
(
    [id] => 
    [greeting] => Nǐ hǎo
    [language] => Chinese
    [_code] =>
)

Notice that

  • the 'continent' array entry has not been bound to the table object, since 'continent' is not a property of the object.
  • the 'id' array entry has not been bound to the table object either, since 'id' was on the ignore list.
  • the '_code' array entry has not been bound to the table because it is a private property of the table object (its name begins with an underscore).