@omnifyjp/ts 5.0.1 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -504,7 +504,21 @@ function buildRelations(schemaName, properties, propertyOrder, modelNamespace, r
|
|
|
504
504
|
const target = prop['target'] ?? '';
|
|
505
505
|
const ns = target ? reader.resolveModelNamespace(target, modelNamespace) : modelNamespace;
|
|
506
506
|
const targetTableName = target ? reader.getTableName(target) : '';
|
|
507
|
-
|
|
507
|
+
// For ManyToMany, look up the explicit `kind: pivot` schema (the
|
|
508
|
+
// sorted PascalCase concat of source + target — auto-created on
|
|
509
|
+
// first generate) and pass its property names so belongsToMany
|
|
510
|
+
// emits `->withPivot('col1', 'col2')`. Without this, custom pivot
|
|
511
|
+
// columns the user added are unreachable through the relation.
|
|
512
|
+
const relation = prop['relation'] ?? '';
|
|
513
|
+
let pivotProperties;
|
|
514
|
+
if (relation === 'ManyToMany' && target) {
|
|
515
|
+
const pivotName = [schemaName, target].sort().join('');
|
|
516
|
+
const pivotSchema = reader.getSchema(pivotName);
|
|
517
|
+
if (pivotSchema?.properties) {
|
|
518
|
+
pivotProperties = Object.keys(pivotSchema.properties).map(toSnakeCase);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const result = buildRelation(propName, prop, ns, { sourceTableName, targetTableName, pivotProperties });
|
|
508
522
|
if (result) {
|
|
509
523
|
methods.push('\n' + result.method);
|
|
510
524
|
declaredMethods.add(toCamelCase(propName));
|
|
@@ -8,6 +8,12 @@ interface RelationResult {
|
|
|
8
8
|
interface RelationContext {
|
|
9
9
|
sourceTableName: string;
|
|
10
10
|
targetTableName: string;
|
|
11
|
+
/** Property names declared on the explicit `kind: pivot` schema for
|
|
12
|
+
* this M2M (e.g. `["position", "notes"]`). Surfaced in the generated
|
|
13
|
+
* `belongsToMany()` chain via `->withPivot(...)` so consumers can
|
|
14
|
+
* read those columns through the relation. Empty / undefined when
|
|
15
|
+
* the user has not added custom columns to the pivot schema. */
|
|
16
|
+
pivotProperties?: string[];
|
|
11
17
|
}
|
|
12
18
|
/** Build Eloquent relation method PHP code. */
|
|
13
19
|
export declare function buildRelation(propName: string, property: Record<string, unknown>, modelNamespace: string, context?: RelationContext): RelationResult | null;
|
|
@@ -128,12 +128,15 @@ function belongsToMany(propName, property, target, ns, context) {
|
|
|
128
128
|
joinTable = generatePivotTableName(context.sourceTableName, context.targetTableName);
|
|
129
129
|
}
|
|
130
130
|
const methodName = toCamelCase(propName);
|
|
131
|
-
const pivotFields = property['pivotFields'] ?? {};
|
|
132
131
|
const fqcn = `\\${ns}\\${target}`;
|
|
133
132
|
let chain = `$this->belongsToMany(${fqcn}::class, '${joinTable}')`;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
// withPivot for every column declared on the explicit `kind: pivot`
|
|
134
|
+
// schema (resolved upstream and passed via context.pivotProperties).
|
|
135
|
+
// The legacy `pivotFields:` block on the M2M property was removed in
|
|
136
|
+
// v5.0.0; pivot columns now live on the pivot schema itself.
|
|
137
|
+
const pivotCols = context?.pivotProperties ?? [];
|
|
138
|
+
if (pivotCols.length > 0) {
|
|
139
|
+
const fields = pivotCols.map(k => `'${toSnakeCase(k)}'`).join(', ');
|
|
137
140
|
chain += `\n ->withPivot(${fields})`;
|
|
138
141
|
}
|
|
139
142
|
chain += `\n ->withTimestamps()`;
|