JTable/bind
(New page: {{review}} ===Syntax=== ''boolean'' bind ($from, [$ignore = array()]) === Parameters === {| class="wikitable" !Argument !Data type !Description !Default |- |$from |array ''or'' objec...) |
|||
| Line 23: | Line 23: | ||
=== Returns === | === Returns === | ||
'''TRUE''' if bind was successfull | '''TRUE''' if bind was successfull | ||
| + | |||
'''FALSE''' if $from is neither an array nor an object and thus cannot be bound to the Table Object | '''FALSE''' if $from is neither an array nor an object and thus cannot be bound to the Table Object | ||
| Line 75: | Line 76: | ||
</source> | </source> | ||
| − | 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 | + | 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. |
===See also=== | ===See also=== | ||
Revision as of 17:42, 11 November 2008
| This is a article which: needs review. You can help the Joomla! Documentation Wiki by contributing to it. More pages that need help similar to this one are here. If you feel the need is satistified, please remove this notice. While actively editing, consider adding {{inuse}} to reduce edit conflicts. |
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 either, since 'id' has been on the ignore list.