X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable.php;h=79669115471813dd4bc3828f4089c4d7543ef4e3;hp=2e82a214b37d89c90815aded33d340616f222005;hb=5c1b63644ccb9277f4dec5e8a14ab6592f1677c5;hpb=b5e596ceb0d6f8e26ce2c98f2b185c03ba4449da diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index 2e82a21..7966911 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -37,13 +37,20 @@ class Table */ protected $exec; + /** + * @var array + */ + protected $dependents; + /** * @param string $name * @param \pq\Connection $conn + * @param array $dependents */ - function __construct($name, \pq\Connection $conn = null) { + function __construct($name, \pq\Connection $conn = null, array $dependents = array()) { $this->name = $name; $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection; + $this->dependents = $dependents; } /** @@ -172,6 +179,43 @@ class Table $query->write("OFFSET", $offset); return $this->execute($query); } + + /** + * Get the parent row of a row by foreign key + * @param \pq\Gateway\Row $dependent + * @param string $name optional fkey name + * @param string $order + * @param int $limit + * @param int $offset + * @return mixed + */ + function of(Row $dependent, $name = null, $order = null, $limit = 0, $offset = 0) { + if (!$name) { + $name = $dependent->getTable()->getName(); + } + return $this->find(array("{$name}_id=" => $dependent->id), + $order, $limit, $offset); + } + + /** + * Get the child rows of a row by foreign key + * @param \pq\Gateway\Row $me + * @param string $dependent + * @param string $order + * @param int $limit + * @param int $offset + * @return mixed + * @throws \LogicException + */ + function by(Row $me, $dependent, $order = null, $limit = 0, $offset = 0) { + if (!isset($this->dependents[$dependent])) { + throw new \LogicException("Unknown dependent table $dependent"); + } + + $dependentClass = $this->dependents[$dependent]; + $dependentModel = new $dependentClass($this->conn); + return $dependentModel->of($me, null, $order, $limit, $offset); + } /** * Insert a row into the table