Skip to content

Commit e3b5232

Browse files
authored
Merge pull request #4589 from najdanovicivan/model-id
BaseModel - Add public getIdValue() method
2 parents 889de06 + 0cb97b3 commit e3b5232

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

system/BaseModel.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,26 @@ abstract protected function doErrors();
463463
* @param array|object $data Data
464464
*
465465
* @return integer|array|string|null
466+
*
467+
* @deprecated Add an override on getIdValue() instead. Will be removed in version 5.0.
466468
*/
467469
abstract protected function idValue($data);
468470

471+
/**
472+
* Public getter to return the id value using the idValue() method
473+
* For example with SQL this will return $data->$this->primaryKey
474+
*
475+
* @param array|object $data
476+
*
477+
* @return array|integer|string|null
478+
*
479+
* @todo: Make abstract in version 5.0
480+
*/
481+
public function getIdValue($data)
482+
{
483+
return $this->idValue($data);
484+
}
485+
469486
/**
470487
* Override countAllResults to account for soft deleted accounts.
471488
* This methods works only with dbCalls
@@ -663,7 +680,7 @@ public function save($data): bool
663680

664681
if ($this->shouldUpdate($data))
665682
{
666-
$response = $this->update($this->idValue($data), $data);
683+
$response = $this->update($this->getIdValue($data), $data);
667684
}
668685
else
669686
{
@@ -687,7 +704,7 @@ public function save($data): bool
687704
*/
688705
protected function shouldUpdate($data) : bool
689706
{
690-
return ! empty($this->idValue($data));
707+
return ! empty($this->getIdValue($data));
691708
}
692709

693710
/**

system/Model.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,22 @@ protected function doErrors()
459459
* @param array|object $data Data
460460
*
461461
* @return integer|array|string|null
462+
*
463+
* @deprecated Use getIdValue() instead. Will be removed in version 5.0.
462464
*/
463465
protected function idValue($data)
466+
{
467+
return $this->getIdValue($data);
468+
}
469+
470+
/**
471+
* Returns the id value for the data array or object
472+
*
473+
* @param array|object $data Data
474+
*
475+
* @return integer|array|string|null
476+
*/
477+
public function getIdValue($data)
464478
{
465479
if (is_object($data) && isset($data->{$this->primaryKey}))
466480
{
@@ -636,7 +650,7 @@ protected function shouldUpdate($data) : bool
636650
return parent::shouldUpdate($data) &&
637651
($this->useAutoIncrement
638652
? true
639-
: $this->where($this->primaryKey, $this->idValue($data))->countAllResults() === 1
653+
: $this->where($this->primaryKey, $this->getIdValue($data))->countAllResults() === 1
640654
);
641655
}
642656

user_guide_src/source/changelogs/v4.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Deprecations:
5252
- Deprecated ``Time::instance()``, use ``Time::createFromInstance()`` instead (now accepts ``DateTimeInterface``).
5353
- Deprecated ``IncomingRequest::removeRelativeDirectory()``, use ``URI::removeDotSegments()`` instead
5454
- Deprecated ``\API\ResponseTrait::failValidationError`` to use ``\API\ResponseTrait::failValidationErrors`` instead
55+
- Deprecated ``BaseModel::idValue()`` in favor of public ``BaseModel::getIdValue()``. ``BaseModel::getIdValue()`` will be defined as abstract in future any custom Model classes should be updated.
5556

5657
Bugs Fixed:
5758

0 commit comments

Comments
 (0)