4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Database\Reflection;
18
* Reflection metadata class for a database.
23
class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
36
* Create conventional structure.
37
* @param string %s stands for table name
38
* @param string %1$s stands for key used after ->, %2$s for table name
39
* @param string %1$s stands for key used after ->, %2$s for table name
41
public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
43
$this->primary = $primary;
44
$this->foreign = $foreign;
45
$this->table = $table;
49
public function getPrimary($table)
51
return sprintf($this->primary, $this->getColumnFromTable($table));
55
public function getHasManyReference($table, $key)
57
$table = $this->getColumnFromTable($table);
59
sprintf($this->table, $key, $table),
60
sprintf($this->foreign, $table, $key),
65
public function getBelongsToReference($table, $key)
67
$table = $this->getColumnFromTable($table);
69
sprintf($this->table, $key, $table),
70
sprintf($this->foreign, $key, $table),
75
protected function getColumnFromTable($name)
77
if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {