我们已经继承了Joomla项目,需要为产品图片实现重复字段。
我们在理解如何在数据库中实现这一点时遇到了麻烦。是否有一些命名约定来告诉Joomla使用什么表/外键?
我们已经在xml文件中找到了:
<?xml version="1.0" encoding="UTF-8"?>
<field
name="gallery_images"
type="subform"
label="Gallery Images"
description="Images for the gallery"
multiple="true"
min="1"
max="10"
>
<form>
<field
name="image_url"
type="media"
label="Image"
/>
</form>
</field>
然后我们将像这样被拉入edit.php文件的形式:
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('gallery_images'); ?></div>
<div class="controls"><?php echo $this->form->getInput('gallery_images'); ?></div>
</div>
实际上,所有这些都可以在后端完美地工作。但这并没有保存任何地方。我们尝试在products表上创建一个字段,以为可能会将其存储为JSON。我们尝试为图像创建另一个带有product_id的表,以为Joomla可以算出外键?
Joomla文档似乎没有说明它在数据库中的工作方式。 (https://docs.joomla.org/Subform_form_field_type)
提前感谢。
因此,由于joomla将gallery_images字段保存到其中,我们编写了一些代码来手动执行此操作。
class ModelProduct extends JModelAdmin
{
public function save($data) {
if (parent::save($data)) {
$registry = new Registry;
$registry->loadArray($data['gallery_image']);
$data['gallery_image'] = (string) $registry;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
$db->quoteName('gallery_image') . ' = ' . $db->quote($data['gallery_image']),
);
// Conditions for which records should be updated.
$conditions = array(
$db->quoteName('id') . ' = '.$data['id'],
);
$query->update($db->quoteName('v4yn2_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
return true;
}
return false;
}
}