Skip to content

Commit

Permalink
Better relation and null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 6, 2015
1 parent 42698d1 commit 9c40677
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function generateFieldAnnotations($className, $fieldName)
}
}

if (CardinalitiesExtractor::CARDINALITY_1_1 === $field['cardinality'] || CardinalitiesExtractor::CARDINALITY_1_N === $field['cardinality']) {
if (!$field['isNullable']) {
$asserts[] = '@Assert\NotNull';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,28 @@ public function generateFieldAnnotations($className, $fieldName)

case CardinalitiesExtractor::CARDINALITY_UNKNOWN:
// No break
case CardinalitiesExtractor::CARDINALITY_0_N:
case CardinalitiesExtractor::CARDINALITY_N_0:
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range']));
break;

case CardinalitiesExtractor::CARDINALITY_1_N:
case CardinalitiesExtractor::CARDINALITY_N_1:
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range']));
$annotations[] = '@ORM\JoinColumn(nullable=false)';
break;

case CardinalitiesExtractor::CARDINALITY_0_N:
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
$annotations[] = '@ORM\JoinTable(inverseJoinColumns={@ORM\JoinColumn(unique=true)})';
break;

case CardinalitiesExtractor::CARDINALITY_1_N:
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
$annotations[] = '@ORM\JoinTable(inverseJoinColumns={@ORM\JoinColumn(nullable=false, unique=true)})';
break;

case CardinalitiesExtractor::CARDINALITY_N_N:
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function generateSetterAnnotations($className, $fieldName)
$this->toPhpType($this->classes[$className]['fields'][$fieldName]),
$fieldName
),
'',
'@return $this',
];
}
Expand All @@ -111,6 +112,7 @@ public function generateAdderAnnotations($className, $fieldName)
$this->toPhpType($this->classes[$className]['fields'][$fieldName], true),
$fieldName
),
'',
'@return $this',
];
}
Expand All @@ -128,6 +130,7 @@ public function generateRemoverAnnotations($className, $fieldName)
$this->toPhpType($this->classes[$className]['fields'][$fieldName], true),
$fieldName
),
'',
'@return $this',
];
}
Expand Down
3 changes: 3 additions & 0 deletions src/SchemaOrgModel/CardinalitiesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class CardinalitiesExtractor
const CARDINALITY_0_N = '(0..*)';
const CARDINALITY_1_1 = '(1..1)';
const CARDINALITY_1_N = '(1..*)';
const CARDINALITY_N_0 = '(*..0)';
const CARDINALITY_N_1 = '(*..1)';
const CARDINALITY_N_N = '(*..*)';
const CARDINALITY_UNKNOWN = 'unknown';

/**
Expand Down
9 changes: 4 additions & 5 deletions src/SchemaOrgModel/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,12 @@ public function generate($config)
}

$isArray = in_array($cardinality, [
CardinalitiesExtractor::CARDINALITY_0_N,
CardinalitiesExtractor::CARDINALITY_1_N,
CardinalitiesExtractor::CARDINALITY_N_N,
]);
$isNullable = in_array($cardinality, [
CardinalitiesExtractor::CARDINALITY_0_1,
CardinalitiesExtractor::CARDINALITY_0_N,
CardinalitiesExtractor::CARDINALITY_UNKNOWN,
$isNullable = !in_array($cardinality, [
CardinalitiesExtractor::CARDINALITY_1_1,
CardinalitiesExtractor::CARDINALITY_1_N,
]);

$class['fields'][$property->localName()] = [
Expand Down
11 changes: 10 additions & 1 deletion src/SchemaOrgModel/TypesGeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ public function getConfigTreeBuilder()
->addDefaultsIfNotSet()
->children()
->scalarNode('range')->defaultNull()->info('The property range')->example('Offer')->end()
->enumNode('cardinality')->defaultValue(CardinalitiesExtractor::CARDINALITY_UNKNOWN)->values([CardinalitiesExtractor::CARDINALITY_0_1, CardinalitiesExtractor::CARDINALITY_0_N, CardinalitiesExtractor::CARDINALITY_1_1, CardinalitiesExtractor::CARDINALITY_1_N, CardinalitiesExtractor::CARDINALITY_UNKNOWN])->end()
->enumNode('cardinality')->defaultValue(CardinalitiesExtractor::CARDINALITY_UNKNOWN)->values([
CardinalitiesExtractor::CARDINALITY_0_1,
CardinalitiesExtractor::CARDINALITY_0_N,
CardinalitiesExtractor::CARDINALITY_1_1,
CardinalitiesExtractor::CARDINALITY_1_N,
CardinalitiesExtractor::CARDINALITY_N_0,
CardinalitiesExtractor::CARDINALITY_N_1,
CardinalitiesExtractor::CARDINALITY_N_N,
CardinalitiesExtractor::CARDINALITY_UNKNOWN
])->end()
->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion templates/class.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ use {{ use }};
* {{ annotation }}
{% endfor %}
*/
public function set{{ field.name|ucfirst }}({% if field.typeHint %}{{ field.typeHint }} {% endif %}${{ field.name }})
public function set{{ field.name|ucfirst }}({% if field.typeHint %}{{ field.typeHint }} {% endif %}${{ field.name }}{% if field.typeHint and field.isNullable %} = null{% endif %})
{
$this->{{ field.name }} = ${{ field.name }};
Expand Down

0 comments on commit 9c40677

Please sign in to comment.