+ if ($offset) {
+ $query->write("OFFSET", $offset);
+ }
+ if ($lock) {
+ $query->write("FOR", $lock);
+ }
+ return $this->execute($query);
+ }
+
+ /**
+ * Get the child rows of a row by foreign key
+ * @param \pq\Gateway\Row $foreign
+ * @param string $name optional fkey name
+ * @param string $order
+ * @param int $limit
+ * @param int $offset
+ * @return mixed
+ */
+ function of(Row $foreign, $name = null, $order = null, $limit = 0, $offset = 0) {
+ // select * from $this where $this->$foreignColumn = $foreign->$referencedColumn
+
+ if (!isset($name)) {
+ $name = $foreign->getTable()->getName();
+ }
+
+ if (!$foreign->getTable()->hasRelation($name, $this->getName())) {
+ return $this->onResult(null);
+ }
+ $rel = $foreign->getTable()->getRelations($name)->{$this->getName()};
+
+ return $this->find(
+ array($rel->foreignColumn . "=" => $foreign->{$rel->referencedColumn}),
+ $order, $limit, $offset
+ );
+ }
+
+ /**
+ * Get the parent rows of a row by foreign key
+ * @param \pq\Gateway\Row $me
+ * @param string $foreign
+ * @param string $order
+ * @param int $limit
+ * @param int $offset
+ * @return mixed
+ */
+ function by(Row $me, $foreign, $order = null, $limit = 0, $offset = 0) {
+ // select * from $foreign where $foreign->$referencedColumn = $me->$foreignColumn
+
+ if (!$this->hasRelation($foreign, $this->getName())) {
+ return $this->onResult(null);
+ }
+ $rel = $this->getRelations($foreign)->{$this->getName()};
+
+ return static::resolve($rel->referencedTable)->find(
+ array($rel->referencedColumn . "=" => $me->{$rel->foreignColumn}),
+ $order, $limit, $offset
+ );
+ }
+
+ /**
+ * Get rows dependent on other rows by foreign keys
+ * @param array $relations
+ * @param array $where
+ * @param string $order
+ * @param int $limit
+ * @param int $offset
+ * @return mixed
+ */
+ function with(array $relations, array $where = null, $order = null, $limit = 0, $offset = 0) {
+ $qthis = $this->conn->quoteName($this->getName());
+ $query = $this->getQueryWriter()->reset();
+ $query->write("SELECT", "$qthis.*", "FROM", $qthis);
+ foreach ($relations as $relation) {
+ $query->write("JOIN", $relation->foreignTable)->write("ON")->criteria(
+ array(
+ "{$relation->referencedTable}.{$relation->referencedColumn}=" =>
+ new QueryExpr("{$relation->foreignTable}.{$relation->foreignColumn}")
+ )
+ );
+ }
+ if ($where) {
+ $query->write("WHERE")->criteria($where);
+ }
+ if ($order) {
+ $query->write("ORDER BY", $order);
+ }
+ if ($limit) {
+ $query->write("LIMIT", $limit);
+ }
+ if ($offset) {
+ $query->write("OFFSET", $offset);
+ }