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/Orm/Field.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/desktop
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Orm;
 12: 
 13: /**
 14:  * ORM Field base class
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Field extends \Kotchasan\Database\Db
 21: {
 22:     /**
 23:      * ชื่อรองของตาราง
 24:      *
 25:      * @var string
 26:      */
 27:     public $table_alias;
 28:     /**
 29:      * ชื่อตาราง
 30:      *
 31:      * @var string
 32:      */
 33:     public $table_name;
 34:     /**
 35:      * ชื่อของการเชื่อมต่อ ใช้สำหรับโหลด config จาก settings/database.php
 36:      *
 37:      * @var string
 38:      */
 39:     protected $conn = 'mysql';
 40:     /**
 41:      * true ถ้ามาจากการ query, false ถ้าเป็นรายการใหม่
 42:      *
 43:      * @var bool
 44:      */
 45:     protected $exists;
 46:     /**
 47:      * ชื่อฟิลด์ที่จะใช้เป็น Primary Key INT(11) AUTO_INCREMENT
 48:      *
 49:      * @var string
 50:      */
 51:     protected $primaryKey = 'id';
 52:     /**
 53:      * ชื่อตาราง
 54:      *
 55:      * @var string
 56:      */
 57:     protected $table;
 58: 
 59:     /**
 60:      * class constructor
 61:      *
 62:      * @param array|object $param ข้อมูลเริ่มต้น
 63:      */
 64:     public function __construct($param = null)
 65:     {
 66:         if (!empty($param)) {
 67:             foreach ($param as $key => $value) {
 68:                 $this->{$key} = $value;
 69:             }
 70:             $this->exists = true;
 71:         } else {
 72:             $this->exists = false;
 73:         }
 74:     }
 75: 
 76:     /**
 77:      * สร้าง record
 78:      *
 79:      * @return \static
 80:      */
 81:     public static function create()
 82:     {
 83:         $obj = new static();
 84:         return $obj;
 85:     }
 86: 
 87:     /**
 88:      * ลบ record
 89:      */
 90:     public function delete()
 91:     {
 92:         $rs = new Recordset(get_called_class());
 93:         return $rs->delete(array($this->primaryKey, (int) $this->{$this->primaryKey}), 1);
 94:     }
 95: 
 96:     /**
 97:      * อ่านค่าตัวแปร conn (ชื่อของการเชื่อมต่อ).
 98:      *
 99:      * @return string
100:      */
101:     public function getConn()
102:     {
103:         return $this->conn;
104:     }
105: 
106:     /**
107:      * ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล
108:      * คืนค่าชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``
109:      *
110:      * @param string $table ชื่อตารางตามที่กำหนดใน settings/datasbase.php
111:      *
112:      * @return string
113:      */
114:     public function getFullTableName($table)
115:     {
116:         $dbname = empty($this->db->settings->dbname) ? '' : '`'.$this->db->settings->dbname.'`.';
117:         $prefix = empty($this->db->settings->prefix) ? '' : $this->db->settings->prefix.'_';
118:         return $dbname.'`'.$prefix.(isset($this->db->tables->$table) ? $this->db->tables->$table : $table).'`';
119:     }
120: 
121:     /**
122:      * คืนค่าชื่อฟิลด์ที่เป็น Primary Key
123:      *
124:      * @return string
125:      */
126:     public function getPrimarykey()
127:     {
128:         return $this->primaryKey;
129:     }
130: 
131:     /**
132:      * อ่านชื่อตาราง
133:      *
134:      * @return string
135:      */
136:     public function getTableName()
137:     {
138:         return $this->table_name;
139:     }
140: 
141:     /**
142:      * อ่านชื่อตารางรวม Alias
143:      *
144:      * @param string|null $alias Alias ที่ต้องการ ถ้าไม่ระบุจะใช้ Alias ตามที่กำหนดไว้
145:      *
146:      * @return string
147:      */
148:     public function getTableWithAlias($alias = null)
149:     {
150:         return $this->table_name.' AS '.(empty($alias) ? $this->table_alias : $alias);
151:     }
152: 
153:     /**
154:      * ฟังก์ชั่นตรวจสอบชื่อตารางและชื่อรอง
155:      *
156:      * @param \Kotchasan\Database\Query $db
157:      */
158:     public function initTableName($db)
159:     {
160:         $this->db = $db;
161:         if (empty($this->table)) {
162:             $class = get_called_class();
163:             if (preg_match('/[a-z0-9]+\\\\([a-z0-9_]+)\\\\Model/i', $class, $match)) {
164:                 $t = strtolower($match[1]);
165:             } elseif (preg_match('/Models\\\\([a-z0-9_]+)/i', $class, $match)) {
166:                 $t = strtolower($match[1]);
167:             } else {
168:                 $t = strtolower($class);
169:             }
170:             $this->table_name = $this->getFullTableName($t);
171:             $this->table_alias = $t;
172:         } elseif (preg_match('/^([a-z0-9A-Z_]+)(\s+(as|AS))?\s+([a-zA-Z0-9]{1,})$/', $this->table, $match)) {
173:             $this->table_name = $this->getFullTableName($match[1]);
174:             $this->table_alias = strlen($match[4]) < 3 ? $match[4] : '`'.$match[4].'`';
175:         } else {
176:             $this->table_name = $this->getFullTableName($this->table);
177:             $this->table_alias = '`'.$this->table.'`';
178:         }
179:     }
180: 
181:     /**
182:      * insert or update record
183:      */
184:     public function save()
185:     {
186:         $rs = new Recordset(get_called_class());
187:         if ($this->exists) {
188:             $rs->update(array($this->primaryKey, (int) $this->{$this->primaryKey}), $this);
189:         } else {
190:             $rs->insert($this);
191:         }
192:     }
193: }
194: 
Kotchasan API documentation generated by ApiGen