Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • DiscoveredConventions
  • StaticConventions

Exceptions

  • AmbiguousReferenceKeyException
  • 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\Conventions;
 9: 
10: use Nette\Database\IConventions;
11: use Nette\Object;
12: 
13: 
14: /**
15:  * Conventions based on static definition.
16:  */
17: class StaticConventions extends Object implements IConventions
18: {
19:     /** @var string */
20:     protected $primary;
21: 
22:     /** @var string */
23:     protected $foreign;
24: 
25:     /** @var string */
26:     protected $table;
27: 
28: 
29:     /**
30:      * Create static conventional structure.
31:      * @param  string %s stands for table name
32:      * @param  string %1$s stands for key used after ->, %2$s for table name
33:      * @param  string %1$s stands for key used after ->, %2$s for table name
34:      */
35:     public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
36:     {
37:         $this->primary = $primary;
38:         $this->foreign = $foreign;
39:         $this->table = $table;
40:     }
41: 
42: 
43:     public function getPrimary($table)
44:     {
45:         return sprintf($this->primary, $this->getColumnFromTable($table));
46:     }
47: 
48: 
49:     public function getHasManyReference($table, $key)
50:     {
51:         $table = $this->getColumnFromTable($table);
52:         return array(
53:             sprintf($this->table, $key, $table),
54:             sprintf($this->foreign, $table, $key),
55:         );
56:     }
57: 
58: 
59:     public function getBelongsToReference($table, $key)
60:     {
61:         $table = $this->getColumnFromTable($table);
62:         return array(
63:             sprintf($this->table, $key, $table),
64:             sprintf($this->foreign, $key, $table),
65:         );
66:     }
67: 
68: 
69:     protected function getColumnFromTable($name)
70:     {
71:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
72:             return $match[1];
73:         }
74: 
75:         return $name;
76:     }
77: 
78: }
79: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0