The following query is executed by the current executor of the table to retrieve the table references:
select
- case when att1.attname like '%\_'||att2.attname then
- substring(att1.attname from '^.*(?=_'||att2.attname||'$)')
- else
- att1.attname
- end as "id"
+ regexp_replace(att1.attname, '_'||att2.attname||'$', '')
+ as "name"
,cl1.relname as "foreignTable"
,att1.attname as "foreignColumn"
,cl2.relname as "referencedTable"
,pg_attribute att1
,pg_attribute att2
where
- ( cl1.relname = \$1
- or cl2.relname = \$1)
+ cl1.relname = \$1
and co.confrelid != 0
and co.conrelid = cl1.oid
and co.conkey[1] = att1.attnum and cl1.oid = att1.attrelid
and co.confrelid = cl2.oid
and co.confkey[1] = att2.attnum and cl2.oid = att2.attrelid
order by
- cl1.relname
- ,att1.attnum
+ att1.attnum
## Cache:
$relations = new Table\Relations(new Table("account_email"));
- // $relations->reference->account
- $account_rel = $relations->getReference("account");
- $account_rel = $relations->account;
+ var_dump($relations->getReference("account", "account"));
+ var_dump($relations->account["account"]);
?>
-# stdClass pq\Gateway\Table\Relations::getReference(string $to)
+# pq\Gateway\Table\Reference pq\Gateway\Table\Relations::getReference(string $table[, string $ref = NULL])
Retrieve the foreign key of the table to another table.
+See pq\Gateway\Table::getRelation().
## Params:
-* string $to
+* string $table
The table name referenced by a foreign key of the table.
+* Optional string $ref = NULL
+ A specific relation id if there are more foreign keys to the same table.
## Returns:
-* stdClass, the table relation.
+* pq\Gateway\Table\Reference, the foreign key.
* NULL, if the tables are not related.
## Example:
Yields:
- object(stdClass)#13 (1) {
- ["account_email"]=>
- object(stdClass)#14 (4) {
- ["foreignTable"]=>
- string(13) "account_email"
- ["foreignColumn"]=>
+ object(pq\Gateway\Table\Reference)#12 (5) {
+ ["name"]=>
+ string(7) "account"
+ ["foreignTable"]=>
+ string(13) "account_email"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
string(10) "account_id"
- ["referencedTable"]=>
- string(7) "account"
- ["referencedColumn"]=>
+ }
+ ["referencedTable"]=>
+ string(7) "account"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
string(2) "id"
}
}
+
<?php
- echo new pq\Gateway\Table("account",
- new pq\Connection("application_name='pq-gateway-docs' connect_timeout=10"));
+ echo new pq\Gateway\Table("account");
?>
Yields:
- postgresql://mike:@:5432/mike?#account
+ postgresql://mike:@:5432/mike#account
-# mixed pq\Gateway\Table::by(pq\Gateway\Row $me, string $foreign[, $order = NULL[, int $limit = 0[, int $offset = 0]]])
+# mixed pq\Gateway\Table::by(pq\Gateway\Row $me[, string $ref = NULL])
-Find rows in another table by foreign key.
+Find the row inthis table referenced by another table's row by foreign key.
See pq\Gateway\Table::find(), pq\Gateway\Table::of() and pq\Gateway\Row::ofWhich().
## Params:
-* pq\Gateway\Row $row
- A row of this table referenced by another table through a foreign key.
-* Optional string $name = NULL
+* pq\Gateway\Row $me
+ A row of another table referenced by this table through a foreign key.
+* Optional string $ref = NULL
The identifying name of the relation.
-* Optional string $order = NULL
- Sorting clause.
-* Optional int $limit = 0
- Row count limit.
-* Optional int $offset = 0
- Row count offset.
+
## Returns:
use pq\Gateway\Table;
- $account_emails = new Table("account_emails");
+ $accounts = new Table("account");
+ $account_emails = new Table("account_email");
$email = $account_emails->find(["email=" => "mike@php.net"])->current();
- $account = $account_emails->by($email, "account")->current();
+ $account1 = $accounts->by($email)->current();
+ $account2 = $email->ofWhich("account")->current();
+
+ var_dump($account1->getData(), $account2->getData());
?>
+
+Yields:
+
+ array(3) {
+ ["id"]=>
+ string(36) "f9710801-c900-4774-848c-22c85deecd83"
+ ["password"]=>
+ NULL
+ ["name"]=>
+ string(4) "mike"
+ }
+ array(3) {
+ ["id"]=>
+ string(36) "f9710801-c900-4774-848c-22c85deecd83"
+ ["password"]=>
+ NULL
+ ["name"]=>
+ string(4) "mike"
+ }
--- /dev/null
+# mixed pq\Gateway\Table::create([array $data = NULL[, string $returning = "*"]])
+
+Create a row in the table.
+
+## Params:
+
+* Optional array $data = NULL
+ The row data.
+* Optional string $returning = "*"
+ What the INSERT should return.
+
+## Returns:
+
+* a [deferred promise of React/Promise](https://github.com/reactphp/promise#deferred-1), when using pq\Query\AsyncExecutor, the asynchronous executor.
+ Else:
+* pq\Result, if pq\Result::$status != pq\Result::TUPLES_OK.
+* pq\Result, if the rowset prototype pq\Gateway\Table::$rowset is empty.
+* pq\Gateway\Rowset, an instance of the rowset prototype.
+
+
+## Example:
+
+ <?php
+
+ use pq\Gateway\Table;
+
+ $accounts = new Table("account");
+ $mike = $accounts->create(["name" => "mike"])->current();
+
+ ?>
--- /dev/null
+# mixed pq\Gateway\Table::delete(array $where,[, $returning = NULL])
+
+Delete rows from the table.
+
+## Params:
+
+* array $where
+ Removal criteria.
+* Optional string $returning = NULL
+ What the DELETE should return.
+
+## Returns:
+
+* a [deferred promise of React/Promise](https://github.com/reactphp/promise#deferred-1), when using pq\Query\AsyncExecutor, the asynchronous executor.
+ Else:
+* pq\Result, if pq\Result::$status != pq\Result::TUPLES_OK.
+* pq\Result, if the rowset prototype pq\Gateway\Table::$rowset is empty.
+* pq\Gateway\Rowset, an instance of the rowset prototype.
--- /dev/null
+# pq\Gateway\Table\Reference pq\Gateway\Table::getRelation(string $table[, string $ref = NULL])
+
+Retrieve the foreign key of the table to another table.
+See pq\Gateway\Table\Relations::getReference().
+
+## Params:
+
+* string $table
+ The table name referenced by a foreign key of the table.
+* Optional string $ref = NULL
+ A specific relation id if there are more foreign keys to the same table.
+
+## Returns:
+
+* pq\Gateway\Table\Reference, the foreign key.
+* NULL, if the tables are not related.
+
+## Example:
+
+ <?php
+
+ use pq\Gateway\Table;
+
+ $email = new Table("account_email");
+ $ref = $email->getRelation("account");
+
+ var_dump($ref);
+
+ ?>
+
+Yields:
+
+ object(pq\Gateway\Table\Reference)#12 (5) {
+ ["name"]=>
+ string(7) "account"
+ ["foreignTable"]=>
+ string(13) "account_email"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
+ string(10) "account_id"
+ }
+ ["referencedTable"]=>
+ string(7) "account"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
+ string(2) "id"
+ }
+ }
+
+
-# \pq\Gateway\Table\Relations|stdClass pq\Gateway\Table::getRelations([string $to = NULL])
+# pq\Gateway\Table\Relations pq\Gateway\Table::getRelations()
Get the relations (by foreign key) of this table.
-See pq\Gateway\Table::hasRelation().
-
-> ***NOTE:***
- The relation name is the column name of the foreign key with the column name of the referenced column cut off the end.
## Params:
-* Optional string $to = NULL
- The table name of which to get the relation to.
+None.
## Returns:
-* stdClass, if $to is given and a relation exists.
-* NULL, if $to is given and a relation does not exist.
-* pq\Gateway\Table\Relations, all relations if $to is omitted.
+* pq\Gateway\Table\Relations, the table's foreign keys.
## Example:
$conn = new pq\Connection;
$conn->exec("
- drop table if exists reftable cascade;
-- drop table if exists account cascade;
- -- drop table if exists account_email cascade;
-
-- create table account (
-- id uuid default uuid_generate_v4() primary key,
-- password char(60),
-- name varchar(68)
-- );
+ -- drop table if exists account_email cascade;
-- create table account_email (
-- account_id uuid not null references account(id) on delete cascade,
-- email varchar(255) not null unique,
-- primary key (account_id, email)
-- );
+ drop table if exists reftable cascade;
create table reftable (
id serial primary key,
- my_account integer references account(id),
- account_id integer references account(id),
- second_account_id integer references account(id),
- email integer references account_email(email)
+ my_account uuid references account(id),
+ account_id uuid references account(id),
+ second_account_id uuid references account(id),
+ email varchar(255) not null references account_email(email)
);
");
Yields:
- object(pq\Gateway\Table\Relations)#10 (1) {
+ object(pq\Gateway\Table\Relations)#7 (1) {
["references":protected]=>
- object(stdClass)#17 (4) {
- ["my_account"]=>
- object(stdClass)#18 (1) {
- ["reftable"]=>
- object(stdClass)#19 (4) {
+ array(2) {
+ ["account"]=>
+ array(3) {
+ ["my_account"]=>
+ object(pq\Gateway\Table\Reference)#14 (5) {
+ ["name"]=>
+ string(10) "my_account"
["foreignTable"]=>
string(8) "reftable"
- ["foreignColumn"]=>
- string(10) "my_account"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
+ string(10) "my_account"
+ }
["referencedTable"]=>
string(7) "account"
- ["referencedColumn"]=>
- string(2) "id"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
+ string(2) "id"
+ }
}
- }
- ["account"]=>
- object(stdClass)#20 (1) {
- ["reftable"]=>
- object(stdClass)#21 (4) {
+ ["account"]=>
+ object(pq\Gateway\Table\Reference)#15 (5) {
+ ["name"]=>
+ string(7) "account"
["foreignTable"]=>
string(8) "reftable"
- ["foreignColumn"]=>
- string(10) "account_id"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
+ string(10) "account_id"
+ }
["referencedTable"]=>
string(7) "account"
- ["referencedColumn"]=>
- string(2) "id"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
+ string(2) "id"
+ }
}
- }
- ["second_account"]=>
- object(stdClass)#22 (1) {
- ["reftable"]=>
- object(stdClass)#23 (4) {
+ ["second_account"]=>
+ object(pq\Gateway\Table\Reference)#16 (5) {
+ ["name"]=>
+ string(14) "second_account"
["foreignTable"]=>
string(8) "reftable"
- ["foreignColumn"]=>
- string(17) "second_account_id"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
+ string(17) "second_account_id"
+ }
["referencedTable"]=>
string(7) "account"
- ["referencedColumn"]=>
- string(2) "id"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
+ string(2) "id"
+ }
}
}
- ["email"]=>
- object(stdClass)#24 (1) {
- ["reftable"]=>
- object(stdClass)#25 (4) {
+ ["account_email"]=>
+ array(1) {
+ ["email"]=>
+ object(pq\Gateway\Table\Reference)#17 (5) {
+ ["name"]=>
+ string(5) "email"
["foreignTable"]=>
string(8) "reftable"
- ["foreignColumn"]=>
- string(5) "email"
+ ["foreignColumns"]=>
+ array(1) {
+ [0]=>
+ string(5) "email"
+ }
["referencedTable"]=>
string(13) "account_email"
- ["referencedColumn"]=>
- string(5) "email"
+ ["referencedColumns"]=>
+ array(1) {
+ [0]=>
+ string(5) "email"
+ }
}
}
}
}
+
+++ /dev/null
-# bool pq\Gateway\Table::hasRelation(string $name[, string $table = NULL])
-
-Check whether a relation to another table exists with the specified name.
-See pq\Gateway\Table\Relations
-
-> ***NOTE:***
- The relation name is the column name of the foreign key with the column name of the referenced column cut off the end.
-
-## Params:
-
-* string $name
- The name of the relation.
-* Optional string $table = NULL
- The bare table name, if the relation has another name (e.g. because of multiple foreign keys to the same table).
-
-## Returns:
-
-* bool, whether the specified relation exists.
-
-## Example:
-
- <?php
-
- use pq\Gateway\Table;
-
- $conn = new pq\Connection;
- $conn->exec("
- drop table if exists account cascade;
- drop table if exists email cascade;
- drop table if exists reftable cascade;
-
- create table account (
- id serial primary key
- );
-
- create table email (
- id serial primary key,
- account_id integer references account(id),
- email text
- );
-
- create table reftable (
- id serial primary key,
- my_account integer references account(id),
- account_id integer references account(id),
- second_account_id integer references account(id),
- email integer references email(id)
- );
- ");
-
- $fgn_table = new Table("reftable");
- var_dump($fgn_table->hasRelation("my_account"));
-
- ?>
-
-Yields:
-
- TRUE
-# mixed pq\Gateway\Table::of(pq\Gateway\Row $foreign[, string $name = NULL[, string $order = NULL[, int $limit = 0[, int $offset = 0]]]])
+# mixed pq\Gateway\Table::of(pq\Gateway\Row $foreign[, string $ref = NULL[, string $order = NULL[, int $limit = 0[, int $offset = 0]]]])
Find rows in table by foreign key.
See pq\Gateway\Table::find(), pq\Gateway\Table::by() and pq\Gateway\Row::allOf().
* pq\Gateway\Row $row
A row of a table referencing this table through a foreign key.
-* Optional string $name = NULL
+* Optional string $ref = NULL
The identifying name of the relation.
* Optional string $order = NULL
Sorting clause.
--- /dev/null
+# mixed pq\Gateway\Table::update(array $where, array $data[, string $returning = "*"])
+
+Update rows in the table.
+See pq\Query\Expr for using SQL constructs as parameters or arguments.
+
+## Params:
+
+* array $where
+ Criteria for the update.
+* array $data
+ Updated row data.
+* Optional string $returniung = "*"
+ What the UPDATE should return.
+
+## Returns:
+
+* a [deferred promise of React/Promise](https://github.com/reactphp/promise#deferred-1), when using pq\Query\AsyncExecutor, the asynchronous executor.
+ Else:
+* pq\Result, if pq\Result::$status != pq\Result::TUPLES_OK.
+* pq\Result, if the rowset prototype pq\Gateway\Table::$rowset is empty.
+* pq\Gateway\Rowset, an instance of the rowset prototype.
+
+## Example:
+
+ <?php
+
+ use pq\Gateway\Table;
+ use pq\Query\Expr;
+
+ $table = new Table("data");
+ $table->update(["id=" => 1], [
+ "foo" => "bar",
+ "ts" => new Expr("NOW()"
+ ]);
+
+ ?>
## Params:
* array $relations
- list of stdClass instances representing relations to join for the query; see pq\Gateway\Table::getRelations().
+ list of pq\Gateway\Table\Reference instances representing relations to join for the query; see pq\Gateway\Table::getRelations().
* Optional array $where = NULL
Additional lookup criteria.
* Optional string $order = NULL
$notifications = new Table("notification");
$relations = [
- $emails->getRelations("account")->account_email,
- $notifications->getRelations("email")->notification
+ $emails->getRelation("account"),
+ $notifications->getRelation("account_email", "email")
];
$bouncers = $accounts->with($relations, ["bounces>" => 3]);