Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

131 add ability to add on delete and on update foreign key constraints in yii2 open api spec definitions #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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