Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * @filesource Kotchasan/Database.php
 4:  *
 5:  * @copyright 2016 Goragod.com
 6:  * @license http://www.kotchasan.com/license/
 7:  *
 8:  * @see http://www.kotchasan.com/
 9:  */
10: 
11: namespace Kotchasan;
12: 
13: /**
14:  * Database class.
15:  *
16:  * @author Goragod Wiriya <admin@goragod.com>
17:  *
18:  * @since 1.0
19:  */
20: final class Database extends KBase
21: {
22:     /**
23:      * database connection instances.
24:      *
25:      * @var array
26:      */
27:     private static $instances = array();
28: 
29:     /**
30:      * Create Database Connection.
31:      *
32:      * @param string|array $name ชื่อของการเชื่อมต่อกำหนดค่าใน config หรือ array ของค่ากำหนดของฐานข้อมูล
33:      *
34:      * @return \static
35:      */
36:     public static function create($name = 'mysql')
37:     {
38:         $param = (object) array(
39:             'settings' => (object) array(
40:                 'driver' => 'PdoMysql',
41:                 'char_set' => 'utf8',
42:                 'dbdriver' => 'mysql',
43:                 'hostname' => 'localhost',
44:             ),
45:             'tables' => (object) array(
46:             ),
47:         );
48:         if (is_string($name)) {
49:             // database config จาก config.php
50:             if (isset(self::$cfg->database[$name]) && is_array(self::$cfg->database[$name])) {
51:                 foreach (self::$cfg->database[$name] as $key => $value) {
52:                     $param->settings->$key = $value;
53:                 }
54:             }
55:             if (empty(self::$instances[$name])) {
56:                 if (is_file(APP_PATH.'settings/database.php')) {
57:                     $config = include APP_PATH.'settings/database.php';
58:                 } elseif (is_file(ROOT_PATH.'settings/database.php')) {
59:                     $config = include ROOT_PATH.'settings/database.php';
60:                 }
61:                 foreach ($config as $key => $values) {
62:                     if ($key == $name) {
63:                         foreach ($values as $k => $v) {
64:                             $param->settings->$k = $v;
65:                         }
66:                     } elseif ($key == 'tables') {
67:                         foreach ($values as $k => $v) {
68:                             $param->tables->$k = $v;
69:                         }
70:                     }
71:                 }
72:             }
73:         } elseif (is_array($name)) {
74:             foreach ($name as $k => $v) {
75:                 $param->settings->$k = $v;
76:             }
77:             $name = rand();
78:         }
79:         if (empty(self::$instances[$name])) {
80:             // โหลด driver (base)
81:             require_once VENDOR_DIR.'Database/Driver.php';
82:             // โหลด driver ตาม config ถ้าไม่พบ ใช้ PdoMysqlDriver
83:             if (is_file(VENDOR_DIR.'Database/'.$param->settings->driver.'Driver.php')) {
84:                 $class = $param->settings->driver.'Driver';
85:             } else {
86:                 // default driver
87:                 $class = 'PdoMysqlDriver';
88:             }
89:             require_once VENDOR_DIR.'Database/'.$class.'.php';
90:             $class = 'Kotchasan\\Database\\'.$class;
91:             self::$instances[$name] = new $class();
92:             self::$instances[$name]->connect($param);
93:         }
94: 
95:         return self::$instances[$name];
96:     }
97: }
98: 
Kotchasan API documentation generated by ApiGen