Column alias: Difference between revisions
From Joomla! Documentation
Explain usage of setColumnAlias method |
m Marked for translation |
||
| Line 1: | Line 1: | ||
The JTable class makes it possible to use column aliases. This can be used for when your database table has a different name than the data going into your table class. | <noinclude><languages /></noinclude> | ||
<translate>The JTable class makes it possible to use column aliases. This can be used for when your database table has a different name than the data going into your table class.</translate> | |||
An example is the state field. Often you will find that the database table has a field called state to keep track of the published/unpublished state of an item. Using the name state within Joomla is not recommended as it can lead to conflicts, instead the name published is used. | <translate>An example is the state field. Often you will find that the database table has a field called state to keep track of the published/unpublished state of an item. Using the name state within Joomla is not recommended as it can lead to conflicts, instead the name published is used.</translate> | ||
How to tell Joomla to store the value of the published form field into the state database field? We do this by using the method setColumnAlias(). | <translate>How to tell Joomla to store the value of the published form field into the state database field? We do this by using the method <tt>setColumnAlias()</tt>.</translate> | ||
We tell JTable that the database table has a field called state and not published. This statement needs to go into the __construct() method of your table class. | <translate>We tell JTable that the database table has a field called state and not published. This statement needs to go into the <tt>__construct()</tt> method of your table class.</translate> | ||
<source lang="php"> | <source lang="php"> | ||
| Line 11: | Line 12: | ||
</source> | </source> | ||
Now when you publish an item the database query will look like this: | <translate>Now when you publish an item the database query will look like this:</translate> | ||
<source lang="sql"> | <source lang="sql"> | ||
| Line 19: | Line 20: | ||
</source> | </source> | ||
As you can see not the field published is set but the state field is set here. | <translate>As you can see not the field published is set but the state field is set here.</translate> | ||
<noinclude> | |||
[[Category:Joomla! 3.x{{#translation:}}]] | |||
[[Category:Component Development{{#translation:}}]] | |||
[[Category:Tutorials{{#translation:}}]] | |||
</noinclude> | |||
Revision as of 05:07, 5 December 2018
The JTable class makes it possible to use column aliases. This can be used for when your database table has a different name than the data going into your table class.
An example is the state field. Often you will find that the database table has a field called state to keep track of the published/unpublished state of an item. Using the name state within Joomla is not recommended as it can lead to conflicts, instead the name published is used.
How to tell Joomla to store the value of the published form field into the state database field? We do this by using the method setColumnAlias().
We tell JTable that the database table has a field called state and not published. This statement needs to go into the __construct() method of your table class.
$this->setColumnAlias('published', 'state');
Now when you publish an item the database query will look like this:
UPDATE prefix_yourtable
SET `state` = 1
WHERE (checked_out = 0 OR checked_out = 748) AND `id` = '72'
As you can see not the field published is set but the state field is set here.