Skip to content
Permalink
Browse files

Selection: typefix for referencing table primary key

  • Loading branch information...
Miloslav Hůla authored and dg committed Apr 11, 2019
1 parent 4e63728 commit a740e3f1afac25e63663705d410711c29b0f644d
@@ -47,10 +47,10 @@ public function __construct(Context $context, IConventions $conventions, string
/**
* Sets active group.
* @internal
* @param int $active primary key of grouped rows
* @param int|string $active primary key of grouped rows
* @return static
*/
public function setActive(int $active)
public function setActive($active)
{
$this->active = $active;
return $this;
@@ -898,9 +898,9 @@ public function getReferencedTable(ActiveRow $row, ?string $table, string $colum
/**
* Returns referencing rows.
* @param int $active primary key
* @param int|string $active primary key
*/
public function getReferencingTable(string $table, string $column = null, int $active = null): ?GroupedSelection
public function getReferencingTable(string $table, string $column = null, $active = null): ?GroupedSelection
{
if (strpos($table, '.') !== false) {
[$table, $column] = explode('.', $table);
@@ -0,0 +1,32 @@
<?php
/**
* Test: Nette\Database\Table: Referencing table PK datatype.
* @dataProvider? ../../databases.ini
*/
declare(strict_types=1);
use Tester\Assert;
require __DIR__ . '/../../connect.inc.php'; // create $connection
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test5.sql");
test(function () use ($context) { // referencing table with integer primary key
$computers = $context->table('room')->get(1000)->related('computer');
Assert::count(1, $computers);
foreach ($computers as $computer) {
Assert::same(1, $computer->id);
}
});
test(function () use ($context) { // referencing table with string primary key
$computers = $context->table('person')->get('mh')->related('computer');
Assert::count(1, $computers);
foreach ($computers as $computer) {
Assert::same(1, $computer->id);
}
});
@@ -0,0 +1,25 @@
DROP DATABASE IF EXISTS nette_test;
CREATE DATABASE nette_test;
USE nette_test;

CREATE TABLE room (
id INTEGER PRIMARY KEY
) ENGINE=InnoDB;

CREATE TABLE person (
username VARCHAR(2) PRIMARY KEY
) ENGINE=InnoDB;

CREATE TABLE computer (
id INTEGER PRIMARY KEY,
room_id INTEGER NOT NULL,
owner_id VARCHAR(2) NOT NULL,
CONSTRAINT room_id FOREIGN KEY (room_id) REFERENCES room (id),
CONSTRAINT owner_id FOREIGN KEY (owner_id) REFERENCES person (username)
) ENGINE=InnoDB;

INSERT INTO room (id) VALUES (1000);

INSERT INTO person (username) VALUES ('mh');

INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
@@ -0,0 +1,22 @@
DROP SCHEMA IF EXISTS public CASCADE;
CREATE SCHEMA public;

CREATE TABLE room (
id INTEGER PRIMARY KEY
);

CREATE TABLE person (
username TEXT PRIMARY KEY
);

CREATE TABLE computer (
id INTEGER PRIMARY KEY,
room_id INTEGER NOT NULL REFERENCES room (id),
owner_id TEXT NOT NULL REFERENCES person (username)
);

INSERT INTO room (id) VALUES (1000);

INSERT INTO person (username) VALUES ('mh');

INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
@@ -0,0 +1,23 @@
DROP TABLE IF EXISTS computer;
DROP TABLE IF EXISTS person;
DROP TABLE IF EXISTS room;

CREATE TABLE room (
id INTEGER PRIMARY KEY
);

CREATE TABLE person (
username TEXT PRIMARY KEY
);

CREATE TABLE computer (
id INTEGER PRIMARY KEY,
room_id INTEGER NOT NULL REFERENCES room (id),
owner_id TEXT NOT NULL REFERENCES person (username)
);

INSERT INTO room (id) VALUES (1000);

INSERT INTO person (username) VALUES ('mh');

INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
@@ -0,0 +1,24 @@
IF OBJECT_ID('computer', 'U') IS NOT NULL DROP TABLE computer;
IF OBJECT_ID('person', 'U') IS NOT NULL DROP TABLE person;
IF OBJECT_ID('room', 'U') IS NOT NULL DROP TABLE room;


CREATE TABLE room (
id INTEGER PRIMARY KEY
);

CREATE TABLE person (
username VARCHAR(2) PRIMARY KEY
);

CREATE TABLE computer (
id INTEGER PRIMARY KEY,
room_id INTEGER NOT NULL REFERENCES room (id),
owner_id VARCHAR(2) NOT NULL REFERENCES person (username)
);

INSERT INTO room (id) VALUES (1000);

INSERT INTO person (username) VALUES ('mh');

INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');

0 comments on commit a740e3f

Please sign in to comment.
You can’t perform that action at this time.