Skip to content

Commit

Permalink
Merge pull request #15158 from zsilbi/relation-params
Browse files Browse the repository at this point in the history
Feature: callable relation params
  • Loading branch information
zsilbi authored Sep 27, 2020
2 parents 3ecfc2a + f97bb2d commit cccda28
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions phalcon/Mvc/Model/Relation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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 @@ -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
]
];
}
]
);
}
}
5 changes: 5 additions & 0 deletions tests/database/Mvc/Model/DeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public function mvcModelDeleteCascadeRelated(DatabaseTester $I)
$customer->paidInvoices->count()
);

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

$I->assertTrue(
$customer->delete()
);
Expand Down

0 comments on commit cccda28

Please sign in to comment.