我终于成功了。由于我花了很多时间,并发现很多人在寻找答案,所以这里是我的解决方法。
我假设您知道如何使用标准MVC结构创建组件:
- 组件入口点
- 组件控制器
- 可能需要的组件路由器
- 组件视图
- 组件模型
- 组件控制器
在模型components\my_component\models\my_model.php中创建自己的保存函数。
public function save($data)
{
$userId = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('user.id');
$user = JFactory::getUser();
$table_one = $this->getTable('TableOne', 'MyComponentTable', array());
$table_two = $this->getTable('TableTwo', 'MyComponentTable', array());
if (!$table_one->bind($data))
{
$this->setError(JText::sprintf('USERS PROFILE BIND FAILED', $user->getError()));
return false;
}
if (!$table_two->bind($data))
{
$this->setError(JText::sprintf('USERS PROFILE BIND FAILED', $user->getError()));
return false;
}
if (!$table_one->save($data))
{
$this->setError($user->getError());
return false;
}
if (!$table_two->save($data))
{
$this->setError($user->getError());
return false;
}
return $user->id;
}
当然,在save函数中需要调用getTable函数
public function getTable($type = 'TableOne', $prefix = 'MyComponentTable', $config = array())
{
$this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
return JTable::getInstance($type, $prefix, $config);
}
并且它起作用了!如此简单! 当然,正如我在问题中所说的那样,整个 $data 被发送到父 save() 函数与不必要的数据,这些数据对 table_one 或 table_two 不重要。 这是在标准 Joomla 结构下工作的方式(没有在代码中进行任何黑客或直接查询)。
希望能有所帮助。