Skip to content

Commit

Permalink
Merge pull request #19 from SOHELAHMED7/131-add-ability-to-add-on-del…
Browse files Browse the repository at this point in the history
…ete-and-on-update-foreign-key-constraints-in-yii2-open-api-spec-definitions

131 add ability to add on delete and on update foreign key constraints in yii2 open api spec definitions
  • Loading branch information
SOHELAHMED7 committed Mar 16, 2023
2 parents 6f4696a + 8d51a34 commit 82f04d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
36 changes: 13 additions & 23 deletions src/lib/openapi/PropertySchema.php
Expand Up @@ -85,36 +85,26 @@ public function __construct(SpecObjectInterface $property, string $name, Compone
$this->schema = $schema;
$this->isPk = $name === $schema->getPkName();

$onUpdate = $onDelete = null;
if (!empty($property->allOf[1]) &&
!empty($property->allOf[1]->{CustomSpecAttr::FK_ON_UPDATE})
) {
$onUpdate = $property->allOf[1]->{CustomSpecAttr::FK_ON_UPDATE};
}
if (!empty($property->allOf[2]) &&
!empty($property->allOf[2]->{CustomSpecAttr::FK_ON_UPDATE})
) {
$onUpdate = $property->allOf[2]->{CustomSpecAttr::FK_ON_UPDATE};
}
$onUpdate = $onDelete = $reference = null;

if (!empty($property->allOf[1]) &&
!empty($property->allOf[1]->{CustomSpecAttr::FK_ON_DELETE})
) {
$onDelete = $property->allOf[1]->{CustomSpecAttr::FK_ON_DELETE};
}
if (!empty($property->allOf[2]) &&
!empty($property->allOf[2]->{CustomSpecAttr::FK_ON_DELETE})
) {
$onDelete = $property->allOf[2]->{CustomSpecAttr::FK_ON_DELETE};
foreach ($property->allOf ?? [] as $element) {
if (!empty($element->{CustomSpecAttr::FK_ON_UPDATE})) {
$onUpdate = $element->{CustomSpecAttr::FK_ON_UPDATE};
}
if (!empty($element->{CustomSpecAttr::FK_ON_DELETE})) {
$onDelete = $element->{CustomSpecAttr::FK_ON_DELETE};
}
if ($element instanceof Reference) {
$reference = $element;
}
}

if (
($onUpdate !== null || $onDelete !== null) &&
(isset($property->allOf[0]) && $property->allOf[0] instanceof Reference)
($reference instanceof Reference)
) {
$this->onUpdateFkConstraint = $onUpdate;
$this->onDeleteFkConstraint = $onDelete;
$this->property = $property->allOf[0];
$this->property = $reference;
$property = $this->property;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/XOnXFkConstraintTest.php
Expand Up @@ -16,7 +16,7 @@

class XOnXFkConstraintTest extends DbTestCase
{
public function testSimple()
public function testIndex()
{
// default DB is Mysql ----------------------------------------
$testFile = Yii::getAlias("@specs/x_on_x_fk_constraint/x_on_x_fk_constraint.php");
Expand Down

0 comments on commit 82f04d4

Please sign in to comment.