+ 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 $ref optional fkey name
+ * @param string $order
+ * @param int $limit
+ * @param int $offset
+ * @return mixed
+ */
+ function of(Row $foreign, $ref = null, $order = null, $limit = 0, $offset = 0) {
+ // select * from $this where $this->$foreignColumn = $foreign->$referencedColumn
+
+ if (!($rel = $this->getRelation($foreign->getTable()->getName(), $ref))) {
+ return $this->onResult(null);
+ }
+
+ $where = array();
+ foreach ($rel as $key => $ref) {
+ $where["$key="] = $foreign->$ref;
+ }
+
+ return $this->find($where, $order, $limit, $offset);
+ }
+
+ /**
+ * Get the parent rows of a row by foreign key
+ * @param \pq\Gateway\Row $foreign
+ * @param string $ref
+ * @return mixed
+ */
+ function by(Row $foreign, $ref = null) {
+ // select * from $this where $this->$referencedColumn = $me->$foreignColumn
+
+ if (!($rel = $foreign->getTable()->getRelation($this->getName(), $ref))) {
+ return $this->onResult(null);
+ }
+
+ $where = array();
+ foreach ($rel as $key => $ref) {
+ $where["$ref="] = $foreign->$key;
+ }
+ return $this->find($where);
+ }
+
+ /**
+ * 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) {
+ if (!($relation instanceof Table\Reference)) {
+ $relation = static::resolve($relation)->getRelation($this->getName());
+ }
+ $query->write("JOIN", $relation->foreignTable)->write("ON");
+ foreach ($relation as $key => $ref) {
+ $query->criteria(
+ array(
+ "{$relation->referencedTable}.{$ref}=" =>
+ new QueryExpr("{$relation->foreignTable}.{$key}")
+ )
+ );
+ }
+ }
+ 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);
+ }