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