Skip to content

Instantly share code, notes, and snippets.

@milo
Created February 20, 2013 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milo/4991664 to your computer and use it in GitHub Desktop.
Save milo/4991664 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . '/Nette/loader.php';
debug();
$db = new Nette\Database\Connection('pgsql:host=localhost;dbname=nette_test', 'postgres');
$db->query('DROP SCHEMA IF EXISTS issue288 CASCADE');
$db->query('CREATE SCHEMA issue288');
$db->query('SET search_path TO issue288');
$db->query('
CREATE TABLE "post" (
"id" INTEGER,
CONSTRAINT "post_pkey" PRIMARY KEY("id")
)
');
$db->query('
CREATE TABLE "tag" (
"id" INTEGER NOT NULL,
CONSTRAINT "tag_pkey" PRIMARY KEY("id")
)
');
$db->query('
CREATE TABLE "post_tag" (
"post_id" INTEGER NOT NULL,
"tag_id" INTEGER NOT NULL,
CONSTRAINT "multi_pk_pkey" PRIMARY KEY("post_id", "tag_id")
)
');
/*
// OR create post_tag in this way (no primary key) => another error
$db->query('
CREATE TABLE "post_tag" (
"post_id" INTEGER NOT NULL,
"tag_id" INTEGER NOT NULL,
CONSTRAINT "post_tag_fk" FOREIGN KEY ("post_id")
REFERENCES "post"("id")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE,
CONSTRAINT "post_tag_fk1" FOREIGN KEY ("tag_id")
REFERENCES "tag"("id")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE
)
');
*/
$factory = new Nette\Database\Table\SelectionFactory($db, new Nette\Database\Reflection\DiscoveredReflection($db));
$db->setSelectionFactory($factory);
$posts = $db->table('post')->where('id',
$db->table('post_tag')->where('tag_id', 3)
);
dump($posts->getSql());
$posts->fetch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment