我从一周开始学习编程,似乎我坚持基础。我想为joomla编写一个插件,它将触发每个用户编辑他的个人资料中的任何数据,它将选择该数据并将其更新到第二个表(它必须同步.Joomla和VirtueMart用户数据)。当你看到我的代码时,希望你的眼睛不会流血,但正如我所说 - 我只是在学习,欢迎每一个批评和提示。
<?php
defined('JPATH_BASE') or die;
class plgUserUserDataSync extends JPlugin
{
public function __construct(&$subject, $params )
{
parent::__construct( $subject, $params );
}
protected function _getUser($user, $options = array())
{
$instance = JUser::getInstance();
if ($id = intval(JUserHelper::getUserId($user['username']))) {
$instance->load($id);
return $instance;
}
function onUserAfterSave($user){
$db = JFactory::getDbo();
$query = $db ->getQuery(true);
$data = array('c.name, c.street, c.postal_code, c.city');
$query->select($db->quoteName($data));
$query->from($db->quoteName('#__XXX_company as c, #__XXX_company_user_map as m'));
$query->where($db->quoteName('m.company_short = c.short and m.user_id=' . (int) $user->id));
$db->setQuery($query);
$results = $db->loadObjectList();
if ($db->getErrorNum()) {
$this->_subject->setError($db->getErrorMsg());
return false;
}
$query = $db ->getQuery(true);
$fields = array(
$db->quoteName('name'),
$db->quoteName('address_1'),
$db->quoteName('zip'),
$db->quoteName('city')
);
$values = $data;
$query->insert($db->quoteName('#__virtuemart_userinfos'))
->columns($db->quoteName($fields))
->values(implode(',', $values));
$db->setQuery($query);
$db->query();
}
}
}
编辑后:
<?php
defined('JPATH_BASE') or die;
class plgUserUserDataSync extends JPlugin
{
public function __construct(&$subject, $params )
{
parent::__construct( $subject, $params );
}
protected function _getUser($user, $options = array())
{
$instance = JUser::getInstance();
if ($id = intval(JUserHelper::getUserId($user['username']))) {
$instance->load($id);
return $instance;
}
function onUserAfterSave($user){
$db = JFactory::getDbo();
$query = $db ->getQuery(true);
$data = array('c.name, c.street, c.postal_code, c.city');
$query->select($db->quoteName($data));
$query->from($db->quoteName('#__XXX_company as c, #__XXX_company_user_map as m'));
$query->where($db->quoteName('m.company_short = c.short and m.user_id=' . (int) $user->id));
$db->setQuery($query);
$results = $db->loadObjectList();
if ($db->getErrorNum()) {
$this->_subject->setError($db->getErrorMsg());
return false;
}
submitData();
}
function submitData(){
$db = JFactory::getDbo();
$query = $db ->getQuery(true);
$fields = array('name, address_1, zip, city');
$query->insert($db->quoteName('#__virtuemart_userinfos'))
->columns($db->quoteName($fields))
->values(implode(',', $results));
$db->setQuery($query);
$db->query();
}
}