当我尝试像下面的代码那样创建自定义主义类型时:
config / packages / doctrine.yaml
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
types:
pickupPointDetails: App\Utils\Types\PickupPointDetailsType
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
PickupPointDetailsType:
<?php
namespace App\Utils\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use App\Utils\Order\PickupPointDetails;
/**
* Class PickupPointDetailsType
*/
class PickupPointDetailsType extends Type
{
const PICKUP_POINT_DETAILS = 'pickupPointDetails';
/**
* @return string
*/
public function getName()
{
return self::PICKUP_POINT_DETAILS;
}
/**
* @param array $fieldDeclaration
* @param AbstractPlatform $platform
*
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'PICKUP_POINT_DETAILS';
}
/**
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return PickupPointDetails
*/
public function convertToPHPValue($value, AbstractPlatform $platform): PickupPointDetails
{
list($name, $city, $informations) = sscanf($value, 'PICKUP_POINT_DETAILS(%s, %s, %s)');
return new PickupPointDetails($name, $city, $informations);
}
/**
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return string
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
$value = sprintf('PICKUP_POINT_DETAILS(%s, %s, %s)', $value->getName(), $value->getCity(), $value->getInformations());
return $value;
}
/**
* @return bool
*/
public function canRequireSQLConversion()
{
return true;
}
// /**
// * {@inheritdoc}
// */
// public function requiresSQLCommentHint(AbstractPlatform $platform)
// {
// return true;
// }
//
// /**
// * @param string $sqlExpr
// * @param AbstractPlatform $platform
// *
// * @return string
// */
// public function convertToPHPValueSQL($sqlExpr, $platform)
// {
// return sprintf('AsText(%s)', $sqlExpr);
// }
//
// /**
// * @param string $sqlExpr
// * @param AbstractPlatform $platform
// *
// * @return string
// */
// public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
// {
// return sprintf('PointFromText(%s)', $sqlExpr);
// }
}
下一步,我将通过DoctrineMigrationsBundle生成迁移,并生成代码:
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190329095621 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE orders ADD pickup_point_details PICKUP_POINT_DETAILS NOT NULL COMMENT \'(DC2Type:pickupPointDetails)\', ADD pickup_point_back_details PICKUP_POINT_DETAILS NOT NULL COMMENT \'(DC2Type:pickupPointDetails)\', DROP pickup_point_data, DROP pickup_point_back_data');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE orders ADD pickup_point_data VARCHAR(255) NOT NULL COLLATE utf8mb4_unicode_ci, ADD pickup_point_back_data VARCHAR(255) NOT NULL COLLATE utf8mb4_unicode_ci, DROP pickup_point_details, DROP pickup_point_back_details');
}
}
执行迁移后,控制台中出现语法错误:
Migrating up to 20190329095621 from 20190329094245
++ migrating 20190329095621
-> ALTER TABLE orders ADD pickup_point_details PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Type:pickupPointDetails)', ADD pickup_point_back_details PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Type:pickupPointDetails)', DROP pickup_point_data, DROP pickup_point_back_data
Migration 20190329095621 failed during Execution. Error An exception occurred while executing 'ALTER TABLE orders ADD pickup_point_details PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Type:pickupPointDetails)', ADD pickup_point_back_details PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Type:pickupPointDetails)', DROP pickup_point_data, DROP pickup_point_back_data':
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Type:pickupPointDetails)', ADD pickup' at line 1
In AbstractMySQLDriver.php line 79:
An exception occurred while executing 'ALTER TABLE orders ADD pickup_point_details PICKUP_POINT_DETAILS NOT NULL C
OMMENT '(DC2Type:pickupPointDetails)', ADD pickup_point_back_details PICKUP_POINT_DETAILS NOT NULL COMMENT '(DC2Ty
pe:pickupPointDetails)', DROP pickup_point_data, DROP pickup_point_back_data':
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual tha
t corresponds to your MySQL server version for the right syntax to use near 'PICKUP_POINT_DETAILS NOT NULL COMMENT
'(DC2Type:pickupPointDetails)', ADD pickup' at line 1
In PDOConnection.php line 90:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual tha
t corresponds to your MySQL server version for the right syntax to use near 'PICKUP_POINT_DETAILS NOT NULL COMMENT
'(DC2Type:pickupPointDetails)', ADD pickup' at line 1
In PDOConnection.php line 88:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual tha
t corresponds to your MySQL server version for the right syntax to use near 'PICKUP_POINT_DETAILS NOT NULL COMMENT
'(DC2Type:pickupPointDetails)', ADD pickup' at line 1
有人知道我做错了吗?
Symfony 4