Skip to content

Commit d46e48d

Browse files
committed
Suggestion
1 parent 8af2578 commit d46e48d

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

en/orm/database-basics.rst

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -532,26 +532,26 @@ implement the following methods:
532532
* ``marshal``: Marshals flat data into PHP objects.
533533

534534
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
536536
class::
537537

538-
// in src/Database/Type/JsonType.php
538+
// in src/Database/Type/PointMutationType.php
539539

540540
namespace App\Database\Type;
541541

542542
use Cake\Database\Driver;
543543
use Cake\Database\Type\BaseType;
544544
use PDO;
545545

546-
class JsonType extends BaseType
546+
class PointMutationType extends BaseType
547547
{
548548
public function toPHP(mixed $value, Driver $driver): mixed
549549
{
550550
if ($value === null) {
551551
return null;
552552
}
553553

554-
return json_decode($value, true);
554+
return $this->pm_decode($value);
555555
}
556556

557557
public function marshal(mixed $value): mixed
@@ -560,12 +560,12 @@ class::
560560
return $value;
561561
}
562562

563-
return json_decode($value, true);
563+
return $this->pm_decode($value);
564564
}
565565

566566
public function toDatabase(mixed $value, Driver $driver): mixed
567567
{
568-
return json_encode($value);
568+
return sprintf('%d%s>%s', $value['position'], $value['from'], $value['to']);
569569
}
570570

571571
public function toStatement(mixed $value, Driver $driver): int
@@ -576,6 +576,19 @@ class::
576576

577577
return PDO::PARAM_STR;
578578
}
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+
}
579592
}
580593

581594
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::
589602

590603
use Cake\Database\TypeFactory;
591604

592-
TypeFactory::map('json', \App\Database\Type\JsonType:class);
605+
TypeFactory::map('point_mutation', \App\Database\Type\PointMutationType:class);
593606

594607
We then have two ways to use our datatype in our models.
595608

@@ -598,25 +611,25 @@ We then have two ways to use our datatype in our models.
598611
and define the SQL column type and reflection logic.
599612

600613
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
602615
Table's :ref:`getSchema() method <saving-complex-types>` add the
603616
following::
604617

605618
class WidgetsTable extends Table
606619
{
607620
public function getSchema(): TableSchemaInterface
608621
{
609-
return parent::getSchema()->setColumnType('widget_prefs', 'json');
622+
return parent::getSchema()->setColumnType('mutation', 'point_mutation');
610623
}
611624
}
612625

613626
Implementing ``ColumnSchemaAwareInterface`` gives you more control over
614627
custom datatypes. This avoids overwriting schema definitions if your
615628
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
617630
used::
618631

619-
// in src/Database/Type/JsonType.php
632+
// in src/Database/Type/PointMutationType.php
620633

621634
namespace App\Database\Type;
622635

@@ -626,7 +639,7 @@ used::
626639
use Cake\Database\Schema\TableSchemaInterface;
627640
use PDO;
628641

629-
class JsonType extends BaseType
642+
class PointMutationType extends BaseType
630643
implements ColumnSchemaAwareInterface
631644
{
632645
// other methods from earlier
@@ -681,8 +694,8 @@ no value for the current database driver:
681694
Mapping Custom Datatypes to SQL Expressions
682695
-------------------------------------------
683696

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
686699
types cannot be represented as strings/integers in SQL queries. When working
687700
with these datatypes your Type class needs to implement the
688701
``Cake\Database\Type\ExpressionTypeInterface`` interface. This interface lets

0 commit comments

Comments
 (0)