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

Model::_checkForeignKeysReverseRestrict() to check relation params #15172

Merged
merged 3 commits into from
Oct 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This component can be used to create SQL statements using a fluent interface. Op
- Fixed `Phalcon\Events\Manager:notifyEvent()` change in the order of notification event: first default event manager, then custom events and finally behaviors, [#14904](https://github.com/phalcon/cphalcon/issues/14904)
- Fixed `Phalcon\Validation\Validator\Uniqueness` fixed except query [#15084](https://github.com/phalcon/cphalcon/issues/15084)
- Fixed `Phalcon\Mvc\Model` to also check the params option in cascade relations when deleting [#15098](https://github.com/phalcon/cphalcon/issues/15098)
- Fixed `Phalcon\Mvc\Model` to also check the params option in restricted relations when deleting [#15172](https://github.com/phalcon/cphalcon/issues/15172)
- Fixed `Phalcon\Mvc\Model::findFirst()` to return correct value [#15077](https://github.com/phalcon/cphalcon/issues/15077)
- Fixed `Phalcon\Mvc\Model\CriteriaInterface::where()` parameters [#15144](https://github.com/phalcon/cphalcon/issues/15144)
- Fixed `Phalcon\Http\Response\Cookies::set()` to utilize the options parameter correctly [#15129](https://github.com/phalcon/cphalcon/issues/15129)
Expand Down
49 changes: 4 additions & 45 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3356,8 +3356,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
{
bool error;
var manager, relations, foreignKey, relation, relationClass,
referencedModel, fields, referencedFields, conditions, bindParams,
position, field, value, extraConditions, message;
fields, message;
int action;

/**
Expand Down Expand Up @@ -3401,50 +3400,10 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
continue;
}

let relationClass = relation->getReferencedModel();
let relationClass = relation->getReferencedModel(),
fields = relation->getFields();

/**
* Load a plain instance from the models manager
*/
let referencedModel = manager->load(relationClass);

let fields = relation->getFields(),
referencedFields = relation->getReferencedFields();

/**
* Create the checking conditions. A relation can has many fields or
* a single one
*/
let conditions = [],
bindParams = [];

if typeof fields == "array" {
for position, field in fields {
fetch value, this->{field};

let conditions[] = "[" . referencedFields[position] . "] = ?" . position,
bindParams[] = value;
}
} else {
fetch value, this->{fields};

let conditions[] = "[" . referencedFields . "] = ?0",
bindParams[] = value;
}

/**
* Check if the virtual foreign key has extra conditions
*/
if fetch extraConditions, foreignKey["conditions"] {
let conditions[] = extraConditions;
}

/**
* We don't trust the actual values in the object and then we're
* passing the values using bound parameters
* Let's make the checking
*/
if referencedModel->count([join(" AND ", conditions), "bind": bindParams]) {
if manager->getRelationRecords(relation, this, null, "count") {
/**
* Create a new message
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/_data/fixtures/models/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ public function initialize()
}
]
);

$this->hasMany(
'cst_id',
Invoices::class,
'inv_cst_id',
[
'alias' => 'inactiveInvoices',
'reusable' => true,
'foreignKey' => [
'action' => Model\Relation::ACTION_RESTRICT
],
'params' => function () {
return [
'inv_status_flag = :inactive:',
'bind' => [
'inactive' => Invoices::STATUS_INACTIVE
]
];
}
]
);
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/database/Mvc/Model/DeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function mvcModelDeleteCascadeRelated(DatabaseTester $I)
$lastName = uniqid('cust-', true);

$customersMigration = new CustomersMigration($connection);
$customersMigration->clear();
$customersMigration->insert($custId, 0, $firstName, $lastName);

$paidInvoiceId = 4;
Expand All @@ -110,6 +111,7 @@ public function mvcModelDeleteCascadeRelated(DatabaseTester $I)
$title = uniqid('inv-');

$invoicesMigration = new InvoicesMigration($connection);
$invoicesMigration->clear();
$invoicesMigration->insert(
$paidInvoiceId,
$custId,
Expand Down Expand Up @@ -159,4 +161,66 @@ public function mvcModelDeleteCascadeRelated(DatabaseTester $I)
$invoices[0]->inv_id
);
}

/**
* Tests Phalcon\Mvc\Model :: delete() with restricted related items
*
* @author Balázs Németh <https://github.com/zsilbi>
* @since 2020-10-17
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelDeleteRestrictRelated(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - delete() with restricted related items');

/** @var PDO $connection */
$connection = $I->getConnection();

$custId = 2;

$firstName = uniqid('cust-', true);
$lastName = uniqid('cust-', true);

$customersMigration = new CustomersMigration($connection);
$customersMigration->clear();
$customersMigration->insert($custId, 0, $firstName, $lastName);

$title = uniqid('inv-');

$invoicesMigration = new InvoicesMigration($connection);
$invoicesMigration->clear();
$invoicesMigration->insert(
1,
$custId,
Invoices::STATUS_INACTIVE,
$title . '-inactive'
);

/**
* @var Customers $customer
*/
$customer = Customers::findFirst($custId);

$I->assertEquals(
1,
$customer->inactiveInvoices->count()
);

$I->assertFalse(
$customer->delete()
);

$I->assertCount(
1,
$customer->getMessages()
);

$I->assertEquals(
'Record is referenced by model ' . Invoices::class,
current($customer->getMessages())->getMessage()
);
}
}