Last active
November 22, 2020 17:11
-
-
Save dustinsgoodman/c234e10cb8794ee9b673105a82380833 to your computer and use it in GitHub Desktop.
PHP Cache Blog - Original Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyObject; | |
use Utils\ArrayUtils as _; // basically lodash | |
class DAO extends \Base\MySQL\DAO { | |
// ... | |
/** | |
* Lookup MyObject entities by relationship A. Can additionally filter by relationship B OR C. | |
* | |
* @param int|int[] $relationAId ID(s) of the main relationship we're looking up | |
* @param int|int[]|null $relationBId ID(s) of relationship B | |
* @param int|int[]|null $relationCId ID(s) of relationship C | |
* | |
* @return MyObject|MyObject[] list of matching records | |
* @throws \Exception | |
*/ | |
public static function getByRelation($relationAId, $relationBId = null, $relationCId = null) { | |
if (empty($relationAId)) { | |
return []; | |
} | |
$query = self::select()->where('relationAId', $relationAId); | |
if (isset($relationBId)) { | |
if (empty($relationBId)) { | |
return []; | |
} | |
$query = $query->where('relationBId', $relationBId); | |
} else if (isset($relationCId)) { | |
if (empty($relationCId)) { | |
return []; | |
} | |
$query = $query->where('relationCId', $relationCId); | |
} | |
$result = $query->toObjects(); | |
if ((!$relationBId && !$relationCId) || is_array($relationAId)) { | |
return $result; | |
} | |
if (count($result) > 1) { | |
throw new \Exception("Ambiguous object for relationA $relationAId, relationB $relationBId"); | |
} | |
return reset($result); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment