JTable/bind
From Joomla! Documentation
< JTable(Difference between revisions)
(Revised and updated.) |
|||
| Line 1: | Line 1: | ||
| − | + | Method to bind an associative array or object to the [[JTable]] instance. This method only binds properties that are publicly accessible and optionally takes an array of properties to ignore when binding. Binding is the process where values are copied into their equivalently named instance properties (see examples). | |
| + | |||
===Syntax=== | ===Syntax=== | ||
| − | ''boolean'' | + | ''boolean'' bind( $from, [$ignore = array()] ) |
=== Parameters === | === Parameters === | ||
| Line 11: | Line 12: | ||
|- | |- | ||
|$from | |$from | ||
| − | |array | + | |array or object |
| − | |An associative array or object | + | |An associative array or object to be bind to the [[JTable]] instance. |
| | | | ||
|- | |- | ||
|$ignore | |$ignore | ||
| − | |array | + | |array or string |
| − | |An array or space separated list of | + | |An optional array or space separated list of properties to ignore while binding. |
| − | | | + | |array() |
|} | |} | ||
| − | + | Returns '''true''' if bind was successful. | |
| − | ''' | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
===Example=== | ===Example=== | ||
| − | + | In this example the class '''TableGreeting''' corresponds to a table called 'jos_greeting' in the Joomla database. | |
<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 41: | ||
</source> | </source> | ||
| − | + | 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"> | ||
| − | // | + | // 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' | ||
); | ); | ||
| − | // | + | // Specify which columns are to be ignored. This can be a string or an array. |
$ignore = 'id'; | $ignore = 'id'; | ||
| − | // | + | // Get the table object from the model. |
| − | $table = $this->getTable('greeting'); | + | $table = $this->getTable( 'greeting' ); |
| − | // | + | |
| − | $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> | |
| − | / | + | This would result in the following output: |
| + | <source lang="text"> | ||
TableGreeting Object | TableGreeting Object | ||
( | ( | ||
| Line 72: | Line 71: | ||
[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). | ||
===See also=== | ===See also=== | ||
| − | * [[JTable/load]] | + | * [http://api.joomla.org/Joomla-Framework/Table/JTable.html#bind JTable->addIncludePath on api.joomla.org] |
| − | * [[JTable/store]] | + | * [[JTable/load|JTable->load]] |
| + | * [[JTable/store|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)]] | * [[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> | <noinclude>[[Category:Development]][[Category:Framework]][[Category:JTable]]</noinclude> | ||
Latest revision as of 13:11, 13 June 2009
Method to bind an associative array or object to the JTable instance. This method only binds properties that are publicly accessible and optionally takes an array of properties to ignore when binding. Binding is the process where values are copied into their equivalently named instance properties (see examples).
Contents |
[edit] Syntax
boolean bind( $from, [$ignore = array()] )
[edit] Parameters
| Argument | Data type | Description | Default |
|---|---|---|---|
| $from | array or object | An associative array or object to be bind to the JTable instance. | |
| $ignore | array or string | An optional array or space separated list of properties to ignore while binding. | array() |
Returns true if bind was successful.
[edit] Example
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).