@@ -540,40 +540,39 @@ implement the following methods:
540
540
* ``marshal ``: Marshals flat data into PHP objects.
541
541
542
542
To fulfill the basic interface, extend :php:class: `Cake\\ Database\\ Type `.
543
- For example if we wanted to add a JSON type, we could make the following type
543
+ For example if we wanted to add a PointMutation type, we could make the following type
544
544
class::
545
545
546
- // in src/Database/Type/JsonType .php
546
+ // in src/Database/Type/PointMutationType .php
547
547
548
548
namespace App\Database\Type;
549
549
550
550
use Cake\Database\Driver;
551
551
use Cake\Database\Type\BaseType;
552
552
use PDO;
553
553
554
- class JsonType extends BaseType
554
+ class PointMutationType extends BaseType
555
555
{
556
556
public function toPHP(mixed $value, Driver $driver): mixed
557
557
{
558
558
if ($value === null) {
559
559
return null;
560
560
}
561
561
562
- return json_decode($value, true);
563
- }
562
+ return $this->pmDecode($valu
564
563
565
564
public function marshal(mixed $value): mixed
566
565
{
567
566
if (is_array($value) || $value === null) {
568
567
return $value;
569
568
}
570
569
571
- return json_decode ($value, true );
570
+ return $this->pmDecode ($value);
572
571
}
573
572
574
573
public function toDatabase(mixed $value, Driver $driver): mixed
575
574
{
576
- return json_encode( $value);
575
+ return sprintf('%d%s>%s', $value['position'], $value['from'], $value['to'] );
577
576
}
578
577
579
578
public function toStatement(mixed $value, Driver $driver): int
@@ -584,6 +583,19 @@ class::
584
583
585
584
return PDO::PARAM_STR;
586
585
}
586
+
587
+ protected function pmDecode(mixed $value): mixed
588
+ {
589
+ if (preg_match('/^(\d+)([a-zA-Z])>([a-zA-Z])$/', $value, $matches)) {
590
+ return [
591
+ 'position' => (int) $matches[1],
592
+ 'from' => $matches[2],
593
+ 'to' => $matches[3]
594
+ ];
595
+ }
596
+
597
+ return null;
598
+ }
587
599
}
588
600
589
601
By default the ``toStatement() `` method will treat values as strings which will
@@ -597,7 +609,8 @@ the type mapping. During our application bootstrap we should do the following::
597
609
598
610
use Cake\Database\TypeFactory;
599
611
600
- TypeFactory::map('json', \App\Database\Type\JsonType::class);
612
+ TypeFactory::map('point_mutation', \App\Database\Type\PointMutationType:class);
613
+
601
614
602
615
We then have two ways to use our datatype in our models.
603
616
@@ -606,27 +619,27 @@ We then have two ways to use our datatype in our models.
606
619
and define the SQL column type and reflection logic.
607
620
608
621
Overwriting the reflected schema with our custom type will enable CakePHP's
609
- database layer to automatically convert JSON data when creating queries. In your
610
- Table's :ref: `initialize() method <saving-complex-types >` add the
622
+ database layer to automatically convert PointMutation data when creating queries. In your
623
+ Table's :ref: `getSchema() method <saving-complex-types >` add the
624
+
611
625
following::
612
626
613
627
class WidgetsTable extends Table
614
628
{
615
629
public function initialize(array $config): void
616
630
{
617
- parent::initialize($config );
631
+ return parent::getSchema()->setColumnType('mutation', 'point_mutation' );
618
632
619
- $this->getSchema()->setColumnType('widget_prefs', 'json');
620
633
}
621
634
}
622
635
623
636
Implementing ``ColumnSchemaAwareInterface `` gives you more control over
624
637
custom datatypes. This avoids overwriting schema definitions if your
625
638
datatype has an unambiguous SQL column definition. For example, we could have
626
- our JSON type be used anytime a ``TEXT `` column with a specific comment is
639
+ our PointMutation type be used anytime a ``TEXT `` column with a specific comment is
627
640
used::
628
641
629
- // in src/Database/Type/JsonType .php
642
+ // in src/Database/Type/PointMutationType .php
630
643
631
644
namespace App\Database\Type;
632
645
@@ -636,7 +649,7 @@ used::
636
649
use Cake\Database\Schema\TableSchemaInterface;
637
650
use PDO;
638
651
639
- class JsonType extends BaseType
652
+ class PointMutationType extends BaseType
640
653
implements ColumnSchemaAwareInterface
641
654
{
642
655
// other methods from earlier
@@ -691,8 +704,8 @@ no value for the current database driver:
691
704
Mapping Custom Datatypes to SQL Expressions
692
705
-------------------------------------------
693
706
694
- The previous example maps a custom datatype for a 'json ' column type which is
695
- easily represented as a string in a SQL statement. Complex SQL data
707
+ The previous example maps a custom datatype for a 'point_mutation ' column type
708
+ which is easily represented as a string in a SQL statement. Complex SQL data
696
709
types cannot be represented as strings/integers in SQL queries. When working
697
710
with these datatypes your Type class needs to implement the
698
711
``Cake\Database\Type\ExpressionTypeInterface `` interface. This interface lets
0 commit comments