Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • ConventionalReflection
  • DiscoveredReflection
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (http://nette.org)
 5:  *
 6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 7:  *
 8:  * For the full copyright and license information, please view
 9:  * the file license.txt that was distributed with this source code.
10:  */
11: 
12: namespace Nette\Database\Reflection;
13: 
14: use Nette;
15: 
16: 
17: 
18: /**
19:  * Reflection metadata class for a database.
20:  *
21:  * @author     Jakub Vrana
22:  * @author     Jan Skrasek
23:  * @property-write Nette\Database\Connection $connection
24:  */
25: class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
26: {
27:     /** @var string */
28:     protected $primary;
29: 
30:     /** @var string */
31:     protected $foreign;
32: 
33:     /** @var string */
34:     protected $table;
35: 
36: 
37: 
38:     /**
39:      * Create conventional structure.
40:      * @param  string %s stands for table name
41:      * @param  string %1$s stands for key used after ->, %2$s for table name
42:      * @param  string %1$s stands for key used after ->, %2$s for table name
43:      */
44:     public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
45:     {
46:         $this->primary = $primary;
47:         $this->foreign = $foreign;
48:         $this->table = $table;
49:     }
50: 
51: 
52: 
53:     public function getPrimary($table)
54:     {
55:         return sprintf($this->primary, $this->getColumnFromTable($table));
56:     }
57: 
58: 
59: 
60:     public function getHasManyReference($table, $key)
61:     {
62:         $table = $this->getColumnFromTable($table);
63:         return array(
64:             sprintf($this->table, $key, $table),
65:             sprintf($this->foreign, $table, $key),
66:         );
67:     }
68: 
69: 
70: 
71:     public function getBelongsToReference($table, $key)
72:     {
73:         $table = $this->getColumnFromTable($table);
74:         return array(
75:             sprintf($this->table, $key, $table),
76:             sprintf($this->foreign, $key, $table),
77:         );
78:     }
79: 
80: 
81: 
82:     public function setConnection(Nette\Database\Connection $connection)
83:     {}
84: 
85: 
86: 
87:     protected function getColumnFromTable($name)
88:     {
89:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '$)', $name, $match)) {
90:             return $match[1];
91:         }
92: 
93:         return $name;
94:     }
95: 
96: }
97: 
Nette Framework 2.0.3 API API documentation generated by ApiGen 2.7.0