Actions

API15

JTable/bind

From Joomla! Documentation

< API15:JTable
Revision as of 16:16, 22 March 2010 by Doxiki (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.

Contents

Description

Binds a named array/hash to this object

[Edit Descripton]

Description:JTable/bind

Syntax

bind($from, $ignore=array())
Parameter Name Default Value Description
$from mixed An associative array or object
$ignore array() mixed An array or space separated list of fields not to bind

Returns

boolean

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

function bind( $from, $ignore=array() )
{
        $fromArray      = is_array( $from );
        $fromObject     = is_object( $from );
 
        if (!$fromArray && !$fromObject)
        {
                $this->setError( get_class( $this ).'::bind failed. Invalid from argument' );
                return false;
        }
        if (!is_array( $ignore )) {
                $ignore = explode( ' ', $ignore );
        }
        foreach ($this->getProperties() as $k => $v)
        {
                // internal attributes of an object are ignored
                if (!in_array( $k, $ignore ))
                {
                        if ($fromArray && isset( $from[$k] )) {
                                $this->$k = $from[$k];
                        } else if ($fromObject && isset( $from->$k )) {
                                $this->$k = $from->$k;
                        }
                }
        }
        return true;
}

[Edit See Also] SeeAlso:JTable/bind

Examples

<CodeExamplesForm /> 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).