1 | <?php |
||
59 | class ManyToMorphedSchema extends AbstractSchema //implements InversableRelationInterface |
||
60 | { |
||
61 | use TypecastTrait, ForeignsTrait, MorphedTrait; |
||
62 | |||
63 | /** |
||
64 | * Relation type. |
||
65 | */ |
||
66 | const RELATION_TYPE = Record::MANY_TO_MORPHED; |
||
67 | |||
68 | /** |
||
69 | * Size of string column dedicated to store outer role name. Used in polymorphic relations. |
||
70 | * Even simple relations might include morph key (usually such relations created via inversion |
||
71 | * of polymorphic relation). |
||
72 | * |
||
73 | * @see RecordSchema::getRole() |
||
74 | */ |
||
75 | const MORPH_COLUMN_SIZE = 32; |
||
76 | |||
77 | /** |
||
78 | * Options to be packed. |
||
79 | */ |
||
80 | const PACK_OPTIONS = [ |
||
81 | Record::PIVOT_TABLE, |
||
82 | Record::OUTER_KEY, |
||
83 | Record::INNER_KEY, |
||
84 | Record::THOUGHT_INNER_KEY, |
||
85 | Record::THOUGHT_OUTER_KEY, |
||
86 | Record::RELATION_COLUMNS, |
||
87 | Record::PIVOT_COLUMNS, |
||
88 | Record::WHERE_PIVOT |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * Default postfix for pivot tables. |
||
93 | */ |
||
94 | const PIVOT_POSTFIX = '_map'; |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | * |
||
99 | * @invisible |
||
100 | */ |
||
101 | const OPTIONS_TEMPLATE = [ |
||
102 | //Inner key of parent record will be used to fill "THOUGHT_INNER_KEY" in pivot table |
||
103 | Record::INNER_KEY => '{source:primaryKey}', |
||
104 | |||
105 | //We are going to use primary key of outer table to fill "THOUGHT_OUTER_KEY" in pivot table |
||
106 | //This is technically "inner" key of outer record, we will name it "outer key" for simplicity |
||
107 | Record::OUTER_KEY => ORMInterface::R_PRIMARY_KEY, |
||
108 | |||
109 | //Name field where parent record inner key will be stored in pivot table, role + innerKey |
||
110 | //by default |
||
111 | Record::THOUGHT_INNER_KEY => '{source:role}_{option:innerKey}', |
||
112 | |||
113 | //Name field where inner key of outer record (outer key) will be stored in pivot table, |
||
114 | //role + outerKey by default |
||
115 | Record::THOUGHT_OUTER_KEY => '{relation:name}_id', |
||
116 | |||
117 | //Declares what specific record pivot record linking to |
||
118 | Record::MORPH_KEY => '{relation:name}_type', |
||
119 | |||
120 | //Set constraints (foreign keys) by default, attention only set for source table |
||
121 | Record::CREATE_CONSTRAINT => true, |
||
122 | |||
123 | //@link https://en.wikipedia.org/wiki/Foreign_key |
||
124 | Record::CONSTRAINT_ACTION => 'CASCADE', |
||
125 | |||
126 | //Relation allowed to create indexes in pivot table |
||
127 | Record::CREATE_INDEXES => true, |
||
128 | |||
129 | //Name of pivot table to be declared, default value is not stated as it will be generated |
||
130 | //based on roles of inner and outer records |
||
131 | Record::PIVOT_TABLE => '{relation:name}_map', |
||
132 | |||
133 | //Relation allowed to create pivot table |
||
134 | Record::CREATE_PIVOT => true, |
||
135 | |||
136 | //Additional set of columns to be added into pivot table, you can use same column definition |
||
137 | //type as you using for your records |
||
138 | Record::PIVOT_COLUMNS => [], |
||
139 | |||
140 | //Set of default values to be used for pivot table |
||
141 | Record::PIVOT_DEFAULTS => [], |
||
142 | |||
143 | //WHERE statement in a form of simplified array definition to be applied to pivot table |
||
144 | //data. |
||
145 | Record::WHERE_PIVOT => [], |
||
146 | ]; |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | * |
||
151 | * Note: pivot table will be build from direction of source, please do not attempt to create |
||
152 | * many to many relations between databases without specifying proper database. |
||
153 | */ |
||
154 | public function declareTables(SchemaBuilder $builder): array |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function packRelation(SchemaBuilder $builder): array |
||
240 | } |