-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
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. |
Guys can we please get this merged in! It's causing a lot issues on our end, we need to use the JSON type! |
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. |
If you need to setup a custom JSON type for Doctrine in Symfony here is how you can achieve it Ensure that 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 <?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')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not assertSame()
?
There was a problem hiding this comment.
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())); | ||
} | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing blank line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot - fixed
Closing in favour of #2653. See description. |
Handled in #2653 |
This follows #2266 , with changes merged into the MySQL57 platform as suggested by @guilhermeblanco and @beberlei.