From 3a35e63feaabc6456a9616e0cd8d3dd05322cb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Bal=C3=A1zs?= Date: Wed, 23 Sep 2020 15:49:00 +0200 Subject: [PATCH 1/2] feat(models): added callback functionality for Relation params --- phalcon/Mvc/Model/Relation.zep | 4 ++++ tests/_data/fixtures/models/Customers.php | 21 +++++++++++++++++++++ tests/database/Mvc/Model/DeleteCest.php | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/phalcon/Mvc/Model/Relation.zep b/phalcon/Mvc/Model/Relation.zep index aafe921a581..a0b75b06e5d 100644 --- a/phalcon/Mvc/Model/Relation.zep +++ b/phalcon/Mvc/Model/Relation.zep @@ -144,6 +144,10 @@ class Relation implements RelationInterface if fetch params, options["params"] { if params { + if is_callable(params) { + return call_user_func(params); + } + return params; } } diff --git a/tests/_data/fixtures/models/Customers.php b/tests/_data/fixtures/models/Customers.php index ae36c6ff788..1c0fb57f465 100644 --- a/tests/_data/fixtures/models/Customers.php +++ b/tests/_data/fixtures/models/Customers.php @@ -65,5 +65,26 @@ public function initialize() ] ] ); + + $this->hasMany( + 'cst_id', + Invoices::class, + 'inv_cst_id', + [ + 'alias' => 'unpaidInvoices', + 'reusable' => true, + 'foreignKey' => [ + 'action' => Model\Relation::NO_ACTION + ], + 'params' => function () { + return [ + 'inv_status_flag = :unpaid:', + 'bind' => [ + 'unpaid' => Invoices::STATUS_UNPAID + ] + ]; + } + ] + ); } } diff --git a/tests/database/Mvc/Model/DeleteCest.php b/tests/database/Mvc/Model/DeleteCest.php index 0e8f414a503..0080c426526 100644 --- a/tests/database/Mvc/Model/DeleteCest.php +++ b/tests/database/Mvc/Model/DeleteCest.php @@ -138,6 +138,11 @@ public function mvcModelDeleteCascadeRelated(DatabaseTester $I) $customer->paidInvoices->count() ); + $I->assertEquals( + 1, + $customer->unpaidInvoices->count() + ); + $I->assertTrue( $customer->delete() ); From f97bb2d099fca85533c287605a79fc995cd43eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Bal=C3=A1zs?= Date: Wed, 23 Sep 2020 17:07:28 +0200 Subject: [PATCH 2/2] docs: changelog [ci skip] --- CHANGELOG-4.1.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-4.1.md b/CHANGELOG-4.1.md index ce885fdab82..59f78392f11 100644 --- a/CHANGELOG-4.1.md +++ b/CHANGELOG-4.1.md @@ -44,6 +44,7 @@ This component can be used to create SQL statements using a fluent interface. Op - Changed the visibility of properties in `Phalcon\Http\Message\Uri` to work with `clone`. [#15040](https://github.com/phalcon/cphalcon/issues/15040) - Change `Phalcon\Validation\AbstractValidator::__construct`. Save custom validator message in options. [#15053](https://github.com/phalcon/cphalcon/issues/15053) [@ivan-zolotavin](https://github.com/ivan-zolotavin) - Add proxy methods without `_` prefix in methods names: `getRelatedRecords()`, `groupResult()`, `exists()`, `preSaveRelatedRecords()`, `preSave()`, `doLowUpdate()`, `postSaveRelatedRecords()`, `postSave()`, `cancelOperation()`, `doLowInsert()`, `getConnection()`, `getConnectionService()`, `getVersion()`, `getSpecial()` [#14971](https://github.com/phalcon/cphalcon/pull/14971) +- Modified `Phalcon\Mvc\Model\Relation` to accept callable params for model relations. [#15158](https://github.com/phalcon/cphalcon/issues/15158) ## Fixed - Fixed `Phalcon\Db\Dialect\Mysql::getColumnDefinition` to recognize `size` for `DATETIME`, `TIME` and `TIMESTAMP` columns [#13297](https://github.com/phalcon/cphalcon/issues/13297)