Skip to content

Commit

Permalink
Merge pull request #436 from hrach/f-database-related
Browse files Browse the repository at this point in the history
Database: implemented short related call related('table.column')
  • Loading branch information
dg committed Dec 28, 2011
2 parents 797fd2c + 64b4d6b commit eed3b1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Nette/Database/Table/ActiveRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public function ref($key, $throughColumn = NULL)
*/
public function related($key, $throughColumn = NULL)
{
if (strpos($key, '.') !== FALSE) {
list($key, $throughColumn) = explode('.', $key);
}

list($table, $column) = $this->table->connection->databaseReflection->getHasManyReference($this->table->name, $key);
$column = $throughColumn ?: $column;
$referencing = $this->table->getReferencingTable($table, $column);
Expand Down
13 changes: 12 additions & 1 deletion tests/Nette/Database/Table.related().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ require_once __DIR__ . '/connect.inc.php';

$apps = array();
foreach ($connection->table('author') as $author) { // SELECT * FROM `author`
foreach ($author->related('book', 'translator_id') as $book) { // SELECT * FROM `book` WHERE (`book`.`author_id` IN (11, 12))
foreach ($author->related('book', 'translator_id') as $book) { // SELECT * FROM `book` WHERE (`book`.`translator_id` IN (11, 12))
$apps[$book->title] = $author->name;
}

foreach ($author->related('book.author_id') as $book) { // SELECT * FROM `book` WHERE (`book`.`author_id` IN (11, 12))
$apps2[$book->title] = $author->name;
}
}

Assert::same(array(
'1001 tipu a triku pro PHP' => 'Jakub Vrana',
'Nette' => 'David Grudl',
'Dibi' => 'David Grudl',
), $apps);

Assert::same(array(
'1001 tipu a triku pro PHP' => 'Jakub Vrana',
'JUSH' => 'Jakub Vrana',
'Nette' => 'David Grudl',
'Dibi' => 'David Grudl',
), $apps2);

0 comments on commit eed3b1c

Please sign in to comment.