如何在控制器joomla中的另一个表中插入值

时间:2014-07-17 06:46:01

标签: joomla

我正在使用joomla 3.1.1和joomshopping。当用户在网站上注册时,我需要在另一个表中同时插入值。在用户控制器中,我需要在自定义表中插入值。我可以在我的控制器文件中使用直接插入查询。这是控制器文件中用于注册用户的功能。我可以把我的代码放在另一个表中插入数据。

 function registersave(){
    JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
    $mainframe = JFactory::getApplication();
    $jshopConfig = JSFactory::getConfig();
    $config = JFactory::getConfig();
    $db = JFactory::getDBO();
    $params = JComponentHelper::getParams('com_users');
    $lang = JFactory::getLanguage();
    $lang->load('com_users');
    $post = JRequest::get('post');
    JPluginHelper::importPlugin('captcha');
    $dispatcher = JDispatcher::getInstance();
    $res = $dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);

    if(!$res[0]){
        JError::raiseWarning('','Invalid Captcha');
        $this->setRedirect("index.php?option=com_jshopping&controller=user&task=register",'','',$jshopConfig->use_ssl);

    }
    else
    {
        JPluginHelper::importPlugin('jshoppingcheckout');
        $dispatcher = JDispatcher::getInstance();

        if ($params->get('allowUserRegistration')==0){
            JError::raiseError( 403, JText::_('Access Forbidden'));
            return;
        }

        $usergroup = JTable::getInstance('usergroup', 'jshop');
        $default_usergroup = $usergroup->getDefaultUsergroup();
         if (!$_POST["id"]){

    }
        $post['username'] = $post['u_name'];
        $post['password2'] = $post['password_2'];
        //$post['name'] = $post['f_name'].' '.$post['l_name'];

        $post['mailing_list'] = $post['mailing_list'];

        $hear = '';

        $post['where_did_you_purchase'] = $post['where_did_you_purchase'];

        $post['ages_of_your_children'] = $agesofchilderen;
        $post['comments_or_suggestions'] = $post['comments_or_suggestions'];
        $post['vehicle_2'] = $post['vehicle_2_model'].'-'.$post['vehicle_2_year'];

        if ($post['birthday']) $post['birthday'] = getJsDateDB($post['birthday'], $jshopConfig->field_birthday_format);

        $dispatcher->trigger('onBeforeRegister', array(&$post, &$default_usergroup));

        $row = JTable::getInstance('userShop', 'jshop');
        $row->bind($post);
        $row->usergroup_id = $default_usergroup;
        $row->password = $post['password'];
        $row->password2 = $post['password2'];

        if (!$row->check("register")){
            JError::raiseWarning('', $row->getError());
            $this->setRedirect(SEFLink("index.php?option=com_jshopping&controller=user&task=register",1,1, $jshopConfig->use_ssl));
            return 0;
        }


        $user = new JUser;
        $data = array();
        $data['groups'][] = $params->get('new_usertype', 2);
        $data['email'] = JRequest::getVar("email");
        $data['password'] = JRequest::getVar("password");
        $data['password2'] = JRequest::getVar("password_2");
        //$data['name'] = $post['f_name'].' '.$post['l_name'];
        $data['username'] = JRequest::getVar("u_name");
        $useractivation = $params->get('useractivation');
        $sendpassword = $params->get('sendpassword', 1);

        if (($useractivation == 1) || ($useractivation == 2)) {
            jimport('joomla.user.helper');
            $data['activation'] = JApplication::getHash(JUserHelper::genRandomPassword());
            $data['block'] = 1;
        }

        //echo $row->getTableName();

        //print_r($row);

        //die("kkk");

        $user->bind($data);
        $user->save();
        $row->user_id = $user->id;
        unset($row->password);
        unset($row->password2);
        if (!$db->insertObject($row->getTableName(), $row, $row->getKeyName())){
            JError::raiseWarning('', "Error insert in table ".$row->getTableName());
            $this->setRedirect(SEFLink("index.php?option=com_jshopping&controller=user&task=register",1,1,$jshopConfig->use_ssl));
            return 0;
        }







    }
} 

1 个答案:

答案 0 :(得分:0)

试试这个,

请不要编辑Joomla核心文件。

如果您需要在自定义表格中添加注册数据,请创建User Plugin。 Joomla在您的情况下提供了大量plugin events,您可以使用onUserAfterSave。事件

使用onUserAfterSave事件创建User plugin,然后只需将Joomla DB库用于自定义表条目。

希望有所帮助......