나는 phalcon에 그러한 옵션이 없다고 가정하기 때문에 코드에 구현해야합니다. 당신은 캐시 주소를 추가 개체 속성을 작성하고 반환을 이미 초기화되어 않다면 수 :
protected $cachedAddresses = null;
public function getAddresses($params = null) {
if ($this->cachedAddresses === null) {
$this->cachedAddresses = $this->getRelated("addresses", array(
"conditions" => "[OrderAddress].active = 'Y'"
));
}
return $this->cachedAddresses;
}
이것은 빠른 해결책이 될 수 있지만, 당신이 다른 관계가 있다면 그것을 반복하는 것이 고통 스러울 것이다 암호. 따라서 DRY를 유지하려면 기본 모델에서 'getRelated'메소드를 다시 정의하여 이미 초기화 된 경우 캐시 된 관계를 반환하려고 시도 할 수 있습니다.
protected $cachedRelations = [];
public function getRelated($name, $params = [], $useCache = true) {
//generate unique cache object id for current arguments,
//so different 'getRelated' calls will return different results, as expected
$cacheId = md5(serialize([$name, $params]));
if (isset($this->cachedRelations[$cacheId]) && $useCache)
return $this->cachedRelations[$cacheId];
else {
$this->cachedRelations[$cacheId] = parent::getRelated($name, $params);
return $this->cachedRelations[$cacheId];
}
}
그런 다음 그대로 'getAddresses'방법을 떠날 수 있으며, 하나의 데이터베이스 쿼리를 수행합니다 그것은 다음과 같이 보일 수 있습니다. 캐시 된 값을 업데이트해야하는 경우에는 false
을 세 번째 매개 변수로 전달하십시오.
그리고 이것은 완전히 테스트되지 않았지만 사소한 오류가 있어도 일반 논리는 분명해야합니다.