joomla-rad icon indicating copy to clipboard operation
joomla-rad copied to clipboard

Images in model

Open Biromain opened this issue 10 years ago • 9 comments

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,

Biromain avatar Oct 08 '15 07:10 Biromain

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.

asika32764 avatar Oct 08 '15 14:10 asika32764

I can PR modifications for docs this weekend, if you want.

Biromain avatar Oct 09 '15 07:10 Biromain

Thank you. Waiting for your PR.

asika32764 avatar Oct 09 '15 08:10 asika32764

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');

Biromain avatar Dec 18 '15 13:12 Biromain

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

asika32764 avatar Dec 18 '15 14:12 asika32764

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()
    }
}

Biromain avatar Dec 18 '15 14:12 Biromain

I will wait Joomla4 (If it drop PHP 5.3 sure) and add this feature to Windwalker.

asika32764 avatar Dec 18 '15 14:12 asika32764

And see if it's MySQL 5.7.8+ for the JSON Data Type

Biromain avatar Dec 18 '15 14:12 Biromain

I know this. I think it can be an option for some developers if they want to use native JSON support

asika32764 avatar Dec 18 '15 14:12 asika32764