Issues (20)

src/Relation/RefersTo.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Cycle ORM Schema Builder.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Schema\Relation;
13
14
use Cycle\ORM\Relation;
15
use Cycle\Schema\Registry;
16
use Cycle\Schema\Relation\Traits\FieldTrait;
17
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
18
19
/**
20
 * Similar to BelongsTo relation but does not force external object to always exists.
21
 */
22
final class RefersTo extends RelationSchema
23
{
24
    use FieldTrait;
25
    use ForeignKeyTrait;
26
27
    // internal relation type
28
    protected const RELATION_TYPE = Relation::REFERS_TO;
29
30
    // relation schema options
31
    protected const RELATION_SCHEMA = [
32
        // save with parent
33
        Relation::CASCADE            => true,
34
35
        // do not pre-load relation by default
36
        Relation::LOAD               => Relation::LOAD_PROMISE,
37
38
        // nullable by default
39
        Relation::NULLABLE           => true,
40
41
        // link to parent entity primary key by default
42
        Relation::INNER_KEY          => '{relation}_{outerKey}',
43
44
        // default field name for inner key
45
        Relation::OUTER_KEY          => '{target:primaryKey}',
46
47
        // rendering options
48
        RelationSchema::INDEX_CREATE => true,
49
        RelationSchema::FK_CREATE    => true,
50
        RelationSchema::FK_ACTION    => 'SET NULL'
51
    ];
52
53
    /**
54
     * @param Registry $registry
55
     */
56
    public function compute(Registry $registry): void
57
    {
58
        parent::compute($registry);
59
60
        $source = $registry->getEntity($this->source);
61
        $target = $registry->getEntity($this->target);
62
63
        // create target outer field
64
        $this->ensureField(
65
            $source,
66
            $this->options->get(Relation::INNER_KEY),
67
            $this->getField($target, Relation::OUTER_KEY),
68
            $this->options->get(Relation::NULLABLE)
0 ignored issues
show
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\RefersTo::ensureField() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
69
        );
70
    }
71
72
    /**
73
     * @param Registry $registry
74
     */
75
    public function render(Registry $registry): void
76
    {
77
        $source = $registry->getEntity($this->source);
78
        $target = $registry->getEntity($this->target);
79
80
        $innerField = $this->getField($source, Relation::INNER_KEY);
81
        $outerField = $this->getField($target, Relation::OUTER_KEY);
82
83
        $table = $registry->getTableSchema($source);
84
85
        if ($this->options->get(self::INDEX_CREATE)) {
86
            $table->index([$innerField->getColumn()]);
87
        }
88
89
        if ($this->options->get(self::FK_CREATE)) {
90
            $this->createForeignKey($registry, $target, $source, $outerField, $innerField);
91
        }
92
    }
93
}
94