Skip to content

Commit a740e3f

Browse files
Miloslav Hůladg
authored andcommitted
Selection: typefix for referencing table primary key
1 parent 4e63728 commit a740e3f

7 files changed

Lines changed: 130 additions & 4 deletions

File tree

src/Database/Table/GroupedSelection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public function __construct(Context $context, IConventions $conventions, string
4747
/**
4848
* Sets active group.
4949
* @internal
50-
* @param int $active primary key of grouped rows
50+
* @param int|string $active primary key of grouped rows
5151
* @return static
5252
*/
53-
public function setActive(int $active)
53+
public function setActive($active)
5454
{
5555
$this->active = $active;
5656
return $this;

src/Database/Table/Selection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,9 +898,9 @@ public function getReferencedTable(ActiveRow $row, ?string $table, string $colum
898898

899899
/**
900900
* Returns referencing rows.
901-
* @param int $active primary key
901+
* @param int|string $active primary key
902902
*/
903-
public function getReferencingTable(string $table, string $column = null, int $active = null): ?GroupedSelection
903+
public function getReferencingTable(string $table, string $column = null, $active = null): ?GroupedSelection
904904
{
905905
if (strpos($table, '.') !== false) {
906906
[$table, $column] = explode('.', $table);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\Table: Referencing table PK datatype.
5+
* @dataProvider? ../../databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../../connect.inc.php'; // create $connection
13+
14+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test5.sql");
15+
16+
17+
test(function () use ($context) { // referencing table with integer primary key
18+
$computers = $context->table('room')->get(1000)->related('computer');
19+
Assert::count(1, $computers);
20+
foreach ($computers as $computer) {
21+
Assert::same(1, $computer->id);
22+
}
23+
});
24+
25+
26+
test(function () use ($context) { // referencing table with string primary key
27+
$computers = $context->table('person')->get('mh')->related('computer');
28+
Assert::count(1, $computers);
29+
foreach ($computers as $computer) {
30+
Assert::same(1, $computer->id);
31+
}
32+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
DROP DATABASE IF EXISTS nette_test;
2+
CREATE DATABASE nette_test;
3+
USE nette_test;
4+
5+
CREATE TABLE room (
6+
id INTEGER PRIMARY KEY
7+
) ENGINE=InnoDB;
8+
9+
CREATE TABLE person (
10+
username VARCHAR(2) PRIMARY KEY
11+
) ENGINE=InnoDB;
12+
13+
CREATE TABLE computer (
14+
id INTEGER PRIMARY KEY,
15+
room_id INTEGER NOT NULL,
16+
owner_id VARCHAR(2) NOT NULL,
17+
CONSTRAINT room_id FOREIGN KEY (room_id) REFERENCES room (id),
18+
CONSTRAINT owner_id FOREIGN KEY (owner_id) REFERENCES person (username)
19+
) ENGINE=InnoDB;
20+
21+
INSERT INTO room (id) VALUES (1000);
22+
23+
INSERT INTO person (username) VALUES ('mh');
24+
25+
INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
DROP SCHEMA IF EXISTS public CASCADE;
2+
CREATE SCHEMA public;
3+
4+
CREATE TABLE room (
5+
id INTEGER PRIMARY KEY
6+
);
7+
8+
CREATE TABLE person (
9+
username TEXT PRIMARY KEY
10+
);
11+
12+
CREATE TABLE computer (
13+
id INTEGER PRIMARY KEY,
14+
room_id INTEGER NOT NULL REFERENCES room (id),
15+
owner_id TEXT NOT NULL REFERENCES person (username)
16+
);
17+
18+
INSERT INTO room (id) VALUES (1000);
19+
20+
INSERT INTO person (username) VALUES ('mh');
21+
22+
INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
DROP TABLE IF EXISTS computer;
2+
DROP TABLE IF EXISTS person;
3+
DROP TABLE IF EXISTS room;
4+
5+
CREATE TABLE room (
6+
id INTEGER PRIMARY KEY
7+
);
8+
9+
CREATE TABLE person (
10+
username TEXT PRIMARY KEY
11+
);
12+
13+
CREATE TABLE computer (
14+
id INTEGER PRIMARY KEY,
15+
room_id INTEGER NOT NULL REFERENCES room (id),
16+
owner_id TEXT NOT NULL REFERENCES person (username)
17+
);
18+
19+
INSERT INTO room (id) VALUES (1000);
20+
21+
INSERT INTO person (username) VALUES ('mh');
22+
23+
INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
IF OBJECT_ID('computer', 'U') IS NOT NULL DROP TABLE computer;
2+
IF OBJECT_ID('person', 'U') IS NOT NULL DROP TABLE person;
3+
IF OBJECT_ID('room', 'U') IS NOT NULL DROP TABLE room;
4+
5+
6+
CREATE TABLE room (
7+
id INTEGER PRIMARY KEY
8+
);
9+
10+
CREATE TABLE person (
11+
username VARCHAR(2) PRIMARY KEY
12+
);
13+
14+
CREATE TABLE computer (
15+
id INTEGER PRIMARY KEY,
16+
room_id INTEGER NOT NULL REFERENCES room (id),
17+
owner_id VARCHAR(2) NOT NULL REFERENCES person (username)
18+
);
19+
20+
INSERT INTO room (id) VALUES (1000);
21+
22+
INSERT INTO person (username) VALUES ('mh');
23+
24+
INSERT INTO computer (id, room_id, owner_id) VALUES (1, 1000, 'mh');

0 commit comments

Comments
 (0)