JSON in Nextras Orm 3.0

Storing an array/std-like object structure as json structure in one db column is pretty common use-case. However, the correct approach in Nextras Orm may not be obvious.

🔝 Use property container 😉

First, create your JSON container, by implementing abstract methods of predefined helper class ImmutableValuePropertyContainer.

use Nette\Utils\Json;
use Nextras\Orm\Entity\ImmutableValuePropertyContainer;

class JsonContainer extends ImmutableValuePropertyContainer
    protected function serialize($value)
        return Json::encode($value); // or simple json_encode()

    protected function deserialize($value)
        return Json::decode($value); // or simple json_decode()

Then, simply define your entity property with container:

 * ...
 * @property Nette\Utils\ArrayHash $data {container JsonContainer}
class Users extends Nextras\Orm\Entity\Entity {}

Please note that ImmutableValuePropertyContainer API may lightly change in the future minor versions.

🆗 The second possible way is to write custom mapping in StorageReflection – add converter callbacks for your specific property.

🙈 The last possibility is to transform value in setters, getters and entity event callbacks. Please do not do this.

no responses for JSON in Nextras Orm 3.0

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.