Namespaces

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

Classes

  • ConventionalReflection
  • DiscoveredReflection

Exceptions

  • AmbiguousReferenceKeyException
  • MissingReferenceException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (https://nette.org)
 5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
 6:  */
 7: 
 8: namespace Nette\Database\Reflection;
 9: 
10: use Nette;
11: 
12: 
13: /**
14:  * Reflection metadata class for a database.
15:  *
16:  * @author     Jakub Vrana
17:  * @author     Jan Skrasek
18:  */
19: class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
20: {
21:     /** @var string */
22:     protected $primary;
23: 
24:     /** @var string */
25:     protected $foreign;
26: 
27:     /** @var string */
28:     protected $table;
29: 
30: 
31:     /**
32:      * Create conventional structure.
33:      * @param  string %s stands for table name
34:      * @param  string %1$s stands for key used after ->, %2$s for table name
35:      * @param  string %1$s stands for key used after ->, %2$s for table name
36:      */
37:     public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
38:     {
39:         $this->primary = $primary;
40:         $this->foreign = $foreign;
41:         $this->table = $table;
42:     }
43: 
44: 
45:     public function getPrimary($table)
46:     {
47:         return sprintf($this->primary, $this->getColumnFromTable($table));
48:     }
49: 
50: 
51:     public function getHasManyReference($table, $key)
52:     {
53:         $table = $this->getColumnFromTable($table);
54:         return array(
55:             sprintf($this->table, $key, $table),
56:             sprintf($this->foreign, $table, $key),
57:         );
58:     }
59: 
60: 
61:     public function getBelongsToReference($table, $key)
62:     {
63:         $table = $this->getColumnFromTable($table);
64:         return array(
65:             sprintf($this->table, $key, $table),
66:             sprintf($this->foreign, $key, $table),
67:         );
68:     }
69: 
70: 
71:     protected function getColumnFromTable($name)
72:     {
73:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
74:             return $match[1];
75:         }
76: 
77:         return $name;
78:     }
79: 
80: }
81: 
Nette 2.1 API documentation generated by ApiGen 2.8.0