diff --git a/src/Row.php b/src/Row.php index 8475aa70..259c35fd 100644 --- a/src/Row.php +++ b/src/Row.php @@ -92,20 +92,7 @@ public function getValue($key) return $this->item->{$this->formatDibiRowKey($key)}; } else if ($this->item instanceof ActiveRow) { - if (preg_match("/^:([a-zA-Z0-9_$]*)\.([a-zA-Z0-9_$]*)$/", $key, $matches)) { - $relatedTable = $matches[1]; - $relatedColumn = $matches[2]; - - return $this->item->related($relatedTable)->fetch()->{$relatedColumn}; - } - - if (preg_match("/^([a-zA-Z0-9_$]*)\.([a-zA-Z0-9_$]*)$/", $key, $matches)) { - $referredTable = $matches[1]; - $referredColumn = $matches[2]; - - return $this->item->ref($referredTable)->{$referredColumn}; - } - return $this->item->{$key}; + return $this->getActiveRowProperty($this->item, $key); } else if ($this->item instanceof Nette\Database\Row) { return $this->item->{$key}; @@ -145,6 +132,37 @@ public function getControlClass() } + /** + * @param ActiveRow $item + * @param string $key + * @return mixed|NULL + */ + public function getActiveRowProperty(ActiveRow $item, $key) + { + if (preg_match("/^:([a-zA-Z0-9_$]+)\.([a-zA-Z0-9_$]+)(:([a-zA-Z0-9_$]+))?$/", $key, $matches)) { + $relatedTable = $matches[1]; + $relatedColumn = $matches[2]; + $throughColumn = isset($matches[4]) ? $matches[4] : NULL; + + $relatedRow = $this->item->related($relatedTable, $throughColumn)->fetch(); + + return $relatedRow ? $relatedRow->{$relatedColumn} : NULL; + } + + if (preg_match("/^([a-zA-Z0-9_$]+)\.([a-zA-Z0-9_$]+)(:([a-zA-Z0-9_$]+))?$/", $key, $matches)) { + $referencedTable = $matches[1]; + $referencedColumn = $matches[2]; + $throughColumn = isset($matches[4]) ? $matches[4] : NULL; + + $referencedRow = $this->item->ref($referencedTable, $throughColumn); + + return $referencedRow ? $referencedRow->{$referencedColumn} : NULL; + } + + return $this->item->{$key}; + } + + /** * LeanMapper: Access object properties to get a item value * @param LeanMapper\Entity $item