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

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 (http://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:  * @property-write Nette\Database\Connection $connection
19:  */
20: class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
21: {
22:     /** @var string */
23:     protected $primary;
24: 
25:     /** @var string */
26:     protected $foreign;
27: 
28:     /** @var string */
29:     protected $table;
30: 
31: 
32:     /**
33:      * Create conventional structure.
34:      * @param  string %s stands for table name
35:      * @param  string %1$s stands for key used after ->, %2$s for table name
36:      * @param  string %1$s stands for key used after ->, %2$s for table name
37:      */
38:     public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
39:     {
40:         $this->primary = $primary;
41:         $this->foreign = $foreign;
42:         $this->table = $table;
43:     }
44: 
45: 
46:     public function getPrimary($table)
47:     {
48:         return sprintf($this->primary, $this->getColumnFromTable($table));
49:     }
50: 
51: 
52:     public function getHasManyReference($table, $key)
53:     {
54:         $table = $this->getColumnFromTable($table);
55:         return array(
56:             sprintf($this->table, $key, $table),
57:             sprintf($this->foreign, $table, $key),
58:         );
59:     }
60: 
61: 
62:     public function getBelongsToReference($table, $key)
63:     {
64:         $table = $this->getColumnFromTable($table);
65:         return array(
66:             sprintf($this->table, $key, $table),
67:             sprintf($this->foreign, $key, $table),
68:         );
69:     }
70: 
71: 
72:     public function setConnection(Nette\Database\Connection $connection)
73:     {}
74: 
75: 
76:     protected function getColumnFromTable($name)
77:     {
78:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
79:             return $match[1];
80:         }
81: 
82:         return $name;
83:     }
84: 
85: }
86: 
Nette 2.0 API documentation generated by ApiGen 2.8.0