How to store user name into db table in backend component

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
tester_e
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Jun 18, 2013 8:06 pm

How to store user name into db table in backend component

Post by tester_e » Tue Jun 18, 2013 8:24 pm

Hello,

I asked the question on stackoverflow. Nobody replied. Please help me find the solution.

My question is: I want to store user name when I save a record in administration back end.

http://stackoverflow.com/questions/1715 ... -component

I have a component being developed for joomla back end administration.

I have an edit view and contoller, model associated with it.

Whenever I edit or create a record, I want to get user name and store it to my table which has a field for current user which makes the update or create the record.

I know how to get user name as:

Code: Select all

$user = JFactory::getUser();
$uname = $user->name;
(component/views//tmpl/edit.php):

Code: Select all

<?php 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$option = JRequest::getCmd('option');

JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo JRoute::_('index.php?option=com_reservation&view=tabletypes'); ?>" method="post" name="adminForm" id="tabletype-admin-form" class="form-validate">
    <input type="hidden" name="option" value="<?=$option?>" />
    <input type="hidden" name="task" value="" />
    <input type="hidden" name="id" value="<?=$this->item->id?>" />
    <input type="hidden" name="upd_user" value="Test-ere" />
    <?php echo JHtml::_('form.token'); ?>

    <fieldset class="adminform">
        <legend>DETAILS</legend>
        <ul class="adminformlist">
                <li><?php echo $this->form->getLabel('name'); ?>
                <?php echo $this->form->getInput('name'); ?></li>
        </ul>
    </fieldset>
</form>
/component/controllers/[controllername].php:

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controlleradmin library
jimport('joomla.application.component.controllerform');
class <ComponentName>Controller<ViewName> extends JControllerForm
{
    public function __construct($config = array())
    {
        $this->view_list = '<viewName>';
        parent::__construct($config);
    }
}
/component/models/[viewname].php

Code: Select all

<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modeladmin' );
class <ComponentName>Model<ViewName> extends JModelAdmin
{    
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form
        $form = $this->loadForm('com_<componentname>.<viewname>', '<viewname>', 
           array('control' => 'jform', 'load_data' => $loadData));
        if (!$form) {
            return false;
        }else{
            return $form;
        }
    }

    public function getTable($type = '<tableName>', $prefix = '<Prefix>', $config = array()) 
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    public function loadFormData()
    {
        // Load form data
        $data = JFactory::getApplication()->getUserState('com_<componentname>.edit.<viewname>.data', array());
        if (empty($data)) 
        {       
            $data = $this->getItem();
        }
        return $data;
    }
}
/component/models/forms/[form].xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset>
                <field
                        name="id"
                        type="hidden"
                />
                <field
                        name="name"
                        type="text"
                        ...
                />
                <field
                        name="published"
                        type="combo"
                        ...
                </field>
                <field
                        name="upd_user" (this is the field where i wanna store current user)
                        type="user"
                />
                <field
                        name="upd_date"
                        type="hidden"
                />
        </fieldset>
</form>
/component/tables/[tablename].php

Code: Select all

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');

// import Joomla table library
jimport('joomla.database.table');

/**
 * Table class
 */
class [ComponentName]Table<ViewName> extends JTable
{
        /**
         * Constructor
         *
         * @param object Database connector object
         */
        function __construct(&$db) 
        {
                parent::__construct('#__[db_table_name]', 'id', $db);
        }
}

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30948
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: How to store user name into db table in backend componen

Post by Per Yngve Berg » Wed Jun 19, 2013 5:53 am

You should use the user id to link your table with the user table, not the user name.

tester_e
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Jun 18, 2013 8:06 pm

Re: How to store user name into db table in backend componen

Post by tester_e » Wed Jun 19, 2013 8:38 am

Can you provide some code snipppets plase?

I am not very good at mvc development.

Thank you.

tester_e
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Jun 18, 2013 8:06 pm

Re: How to store user name into db table in backend componen

Post by tester_e » Sat Jun 22, 2013 8:48 pm

I found the solution.

In the class where you implemented JModelAdmin, fill this method to modify your table fields as you wish:

/**
* Prepare and sanitise the table data prior to saving.
*
* @param JTable &$table A reference to a JTable object.
*
* @return void
*
* @since 11.1
*/
protected function prepareTable(&$table)
{
// Derived class will provide its own implementation if required.
}

drewgg
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 161
Joined: Thu Oct 30, 2008 7:27 pm

Re: How to store user name into db table in backend componen

Post by drewgg » Mon Jun 24, 2013 1:21 pm

You might want to consider creating a User plugin that triggers on "onUserAfterSave" so that you don't have to make changes to the Joomla core (which would require you to reapply your changes after Joomla updates).


Locked

Return to “Joomla! 2.5 Coding”