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
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • DiscoveredConventions
  • StaticConventions

Exceptions

  • AmbiguousReferenceKeyException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
 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;
11: use Nette\Database\IConventions;
12: 
13: 
14: /**
15:  * Conventions based on static definition.
16:  */
17: class StaticConventions implements IConventions
18: {
19:     use Nette\SmartObject;
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 static 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 [
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 [
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: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0