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
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • DiscoveredConventions
  • StaticConventions

Exceptions

  • AmbiguousReferenceKeyException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (http://nette.org)
 5:  * Copyright (c) 2004 David Grudl (http://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:  * @author     Jakub Vrana
18:  * @author     Jan Skrasek
19:  */
20: class StaticConventions extends Object implements IConventions
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 static 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:     protected function getColumnFromTable($name)
73:     {
74:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
75:             return $match[1];
76:         }
77: 
78:         return $name;
79:     }
80: 
81: }
82: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0