Change default queryColumnType and columnType


#1

Hey,

I have to save a large amount of (serialized) data to a hotspotimage and the database type “text” cannot handle this. Therefor I want to change the default (for the whole project!) to “mediumtext”, but I struggled with the changes I have to do.

If I change the (Pimcore\Model\DataObject\ClassDefinition\Data)Hotspot class directly ($queryColumnType & $columnType) and add a new hotspotimage (image galery etc) to my objects definitions this will work as expected (only for tests!). So I tried to overwrite the attributes with my own hotspotimage class (simular to the manual https://pimcore.com/docs/5.x/Development_Documentation/Extending_Pimcore/Overriding_Models.html):

(code)
namespace AppBundle\Model\DataObject\ClassDefinition\Data;

class Hotspotimage extends \Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage
{

/**
 * Type for the column to query
 *
 * @var string
 */
public $queryColumnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];

/**
 * Type for the column
 *
 * @var string
 */
public $columnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];

}
(code)

in my “app/config/config.yml” I added following lines:
(code)
pimcore:
models:
class_overrides:
‘Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage’: ‘AppBundle\Model\DataObject\ClassDefinition\Data\Hotspotimage’
(code)

My own class will not be loaded and therefor my changes will not work.

Is this the right way or is it even possible to do that way? Or is it better to write a own datatype (extending hotspotimage)?

Greetings


#2

try this in your config:

pimcore:
    objects:
        class_definitions:
            data:
                map:
                    customHotspotimage: AppBundle\CoreExtension\Hotspotimage

this is your PHP File:

<?php
//src/AppBundle/CoreExtension/CustomHotspotimage.php

namespace AppBundle\CoreExtension;

class CustomHotspotimage extends \Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage
{

    /**
     * Type for the column to query
     *
     * @var string
     */
    public $queryColumnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];

    /**
     * Type for the column
     *
     * @var string
     */
    public $columnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];
}

add two new JS Files:

//src/AppBundle/Resources/public/js/coreExtension/data/customHotspotimage.js:
pimcore.registerNS("pimcore.object.classes.data.customHotspotimage");
pimcore.object.classes.data.customHotspotimage  = Class.create(pimcore.object.classes.data.hotspotimage, {
});
//src/AppBundle/Resources/public/js/coreExtension/tags/customHotspotimage.js:
pimcore.registerNS("pimcore.object.classes.tags.customHotspotimage");
pimcore.object.classes.data.customHotspotimage  = Class.create(pimcore.object.classes.tags.hotspotimage, {
});

add this to your AppBundle.php

<?php

namespace AppBundle;

use Pimcore\Extension\Bundle\AbstractPimcoreBundle;

class AppBundle extends AbstractPimcoreBundle
{
    public function getNiceName()
    {
        return 'AppBundle';
    }

    public function getJsPaths()
    {
        return [
            '/bundles/app/js/coreExtension/data/customHotspotimage.js',
            '/bundles/app/js/coreExtension/tags/customHotspotimage.js'
        ];
    }
}

Change your class definition to use your CustomHotspotimage instead of the official one.


#3

Hello @dpfaffenbauer,

thank you very much for your reply and your time spent!

I tried to get it work but I still struggle with it. I’ve checked my implementation multiple times so it should be exactly your suggest. (the new data type is added to the menu and can be added to the class definitions, but the db column type is still “text”).

Nethertheless I probably discovered two little mistakes (or are they wanted that way?):

  1. “try this in your config”
    Shouldn’t it be “customHotspotimage: AppBundle\CoreExtension\ CustomHotspotimage” instead of “customHotspotimage: AppBundle\CoreExtension\Hotspotimage”

  2. “add two new JS Files:” (the second file)
    Shouldn’t it be “pimcore.object.classes.tags.customHotspotimage = Class.create(…)”?

In both cases I tried your suggest and my updated version, the result is the same.

After some tries and a lot of time spent, I also checked the object definition class (/var/classes/definition_*.php) and there I found
“Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage::__set_state(array( (…)” instead of the expected(?) “AppBundle\CoreExtension\CustomHotspotimage::__set_state(array( (…)”.
Also I tried to add the attribute “$fieldtype” (class CustomHotspotimage) and/or a “type” attribute to the js-classes - but with no effect.

Afterall I have still no idea what else I can do now :confused:

Tanks & greetings


#4

The solution is to add the “initialize” function to the “pimcore.object.classes.data.customHotspotimage”. It’s required because the parent “initialize” function use a hard coded this.type = “hotspotimage”; instead of the class attribute type (which will be overwritten by my/your own class)

AppBundle/CoreExtension/CustomHotspotimage.php

namespace AppBundle\CoreExtension;
class CustomHotspotimage extends \Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage
{
/**
* Static type of this element
*
* @var string
*/
public $fieldtype = ‘customHotspotimage’;

/**
 * Type for the column to query
 *
 * @var string
 */
public $queryColumnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];

/**
 * Type for the column
 *
 * @var string
 */
public $columnType = ['image' => 'int(11)', 'hotspots' => 'mediumtext'];

}

AppBundle/Resources/public/js/coreExtension/data/customHotspotimage.js

pimcore.registerNS(“pimcore.object.classes.data.customHotspotimage”);
pimcore.object.classes.data.customHotspotimage = Class.create(pimcore.object.classes.data.hotspotimage, {

type: "customHotspotimage",

initialize: function (treeNode, initData) {
    this.type = "customHotspotimage";

    this.initData(initData);

    // overwrite default settings
    this.availableSettingsFields = ["name", "title", "tooltip", "mandatory", "noteditable", "invisible",
        "visibleGridView", "visibleSearch", "style"];

    this.treeNode = treeNode;
},

getTypeName: function () {
    return 'CUSTOM Image Advanced';
},

});

AppBundle/Resources/public/js/coreExtension/tags/customHotspotimage.js

pimcore.registerNS(“pimcore.object.tags.customHotspotimage”);
pimcore.object.tags.customHotspotimage = Class.create(pimcore.object.tags.hotspotimage, {
type: “customHotspotimage”,
});

AppBundle/AppBundle.php

namespace AppBundle;
use Pimcore\Extension\Bundle\AbstractPimcoreBundle;

class AppBundle extends AbstractPimcoreBundle
{
public function getJsPaths() {
return [
‘/bundles/app/js/coreExtension/data/customHotspotimage.js’,
‘/bundles/app/js/coreExtension/tags/customHotspotimage.js’
];
}
}

app/config/config.yml

pimcore:
objects:
class_definitions:
data:
map:
customHotspotimage: AppBundle\CoreExtension\CustomHotspotimage

Sorry, don’t know how to format the code in here correctly…

Update:
Fixed “pimcore.object.tags.customHotspotimage” (no “classes”)


Adding custom object datatypes