Images in model
Hello,
I can see in view :
echo JHtml::_('image', $item->images, $item->title);
Why not manage that like com_content with json by default :
<fields name="images" label="COM_SAKURA_FIELD_IMAGE_OPTIONS">
<field name="intro"
type="media"
label="COM_SAKURA_TREE_INTROPICTURE"
preview="true"
description="" />
</fields>
And in model :
public function getItem($pk = null) {
$item = parent::getItem($pk);
if(property_exists($item, 'images')) {
$registry = new Joomla\Registry\Registry;
$registry->loadString($item->images);
$item->images = $registry->toArray(); // Or toObject()
}
return $item;
}
public function save($data) {
if(!empty($data['images'])) {
$registry = new Joomla\Registry\Registry;
$registry->loadArray($data['images']);
$data['images'] = (string) $registry;
}
return parent::save($data);
}
Regards,
It is just an example. Developers who need a JSON images storage may write their code which like you provided.
We don't write a too complex sample in default template.
I can PR modifications for docs this weekend, if you want.
Thank you. Waiting for your PR.
Better idea (i think), why not implement in Model.php some methods like that?
//Easily convert Registry json data
protected function getRegistryValues($obj, $property) {
$registry = new Registry;
if(property_exists($obj, $property)) {
$registry->loadString($obj->$property);
}
return $registry;
}
protected function jsonToObject($obj, $property) {
$registry = $this->getRegistryValues($obj, $property);
return $registry->count()>0?$registry->toObject():null;
}
protected function jsonToArray($obj, $property) {
$registry = $this->getRegistryValues($obj, $property);
return $registry->count()>0?$registry->toArray():null;
}
// ...
//In component model file, dev can use like that
$item->images = $this->jsonToObject($item, 'images');
You provide a good idea.
But in Windwalker, I prefer do this way.
$item->images = with(new JRegistry($item->images))->toArray();
// OR
$item->images = with(new JRegistry($item->images))->toObject();
It's simple and enough to use, and developer should know they have any fields in table, so they don't need to use property_exists
But if we decided to add these methods, I'd like to implement them in a Trait (like Laravel does now)
class MyModel extends AbstractAnyModel
{
use JsonFieldTraits;
}
Because not everybody want to use same way to manage json data, maybe we shouldn't hard code it in low layer Model class.
The obvious bad point is that we need PHP 5.4 support
Yeah, atm in my component, methods are in trait in src/Model/Utils
Like that?
<?php
namespace MyComponent\Model\Utils;
defined('_JEXEC') or die;
trait JsonFieldTraits {
protected function jsonFieldToObject($fieldValue) {
return with(new JRegistry($fieldValue))->toObject();
}
protected function jsonFieldToArray($fieldValue) {
return with(new JRegistry($fieldValue))->toArray()
}
}
I will wait Joomla4 (If it drop PHP 5.3 sure) and add this feature to Windwalker.
And see if it's MySQL 5.7.8+ for the JSON Data Type
I know this. I think it can be an option for some developers if they want to use native JSON support