Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native JSON type on MySQL >=5.7 #2455

Closed
wants to merge 3 commits into from

Conversation

robhogan
Copy link

@robhogan robhogan commented Jul 24, 2016

This follows #2266 , with changes merged into the MySQL57 platform as suggested by @guilhermeblanco and @beberlei.

@arroyo
Copy link

arroyo commented Aug 15, 2016

Is there any movement on the json type support for mysql. I see these two open pull requests. Looking forward to using this when it's ready. Thanks.

@sameg14
Copy link

sameg14 commented Aug 19, 2016

Guys can we please get this merged in! It's causing a lot issues on our end, we need to use the JSON type!

@robhogan
Copy link
Author

It's been over a month since there were any commits at all to master, but fairly active before that - I guess the key maintainers are away/occupied.

@sameg14
Copy link

sameg14 commented Aug 23, 2016

If you need to setup a custom JSON type for Doctrine in Symfony here is how you can achieve it

Ensure that app/config/config.yml has this line for doctrine

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        server_version: 5.7
        mapping_types:
            json: string
        types:
            custom_json:
                class: CoreBundle\DBAL\Type\JsonType
                commented: true

Then ensure that you create the custom type as such

<?php

namespace CoreBundle\DBAL\Type;

use Doctrine\DBAL\Types\JsonArrayType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * Class JsonType
 * @package CoreBundle\DBAL
 */
class JsonType extends JsonArrayType
{
    /**
     * @param array $fieldDeclaration
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return 'json';
    }

    /**
     * Convert the value of the field into a PHP data type
     * @param mixed $value
     * @param AbstractPlatform $platform
     * @return array|mixed|null
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value === null ? null : parent::convertToPHPValue($value, $platform);
    }

    /**
     * Name of the db type
     * @return string
     */
    public function getName()
    {
        return 'json';
    }
}

Then your entity property should use the custom_json type to store data in mysql using json

<?php

namespace CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @package CoreBundle\ExampleJsonEntity
 * @ORM\Table(name="example_json_table")
 * @ORM\Entity(repositoryClass="CoreBundle\Repository\ExampleJsonRepository")
 */
class ExampleJsonEntity
{
    /**
     * @var int
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="json_data", type="custom_json", nullable=true)
     */
    private $jsonData;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getJsonData()
    {
        return $this->jsonData;
    }

    /**
     * @param string $jsonData
     */
    public function setJsonData($jsonData)
    {
        $this->jsonData = $jsonData;
    }
}

public function testInitializesJsonTypeMapping()
{
$this->assertTrue($this->_platform->hasDoctrineTypeMappingFor('json'));
$this->assertEquals('json_array', $this->_platform->getDoctrineTypeMapping('json'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not assertSame()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - updated.

{
$this->assertSame('JSON', $this->_platform->getJsonTypeDeclarationSQL(array()));
}
/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot - fixed

@deeky666
Copy link
Member

deeky666 commented Feb 9, 2017

Closing in favour of #2653. See description.

@Ocramius
Copy link
Member

Ocramius commented May 9, 2017

Handled in #2653

@Ocramius Ocramius changed the title [MySQL] Use native JSON type on MySQL >=5.7 Use native JSON type on MySQL >=5.7 Jul 22, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants