Helpers\TableBuilder
Helpers\TableBuilder
Table builder helper is a class that would help you to create tables in MySQL (primarily) without really going into details of SQL query.
Features
Table builder allows you to add rows, aliases, set primary key, default values, table name and options.
Start working with Table Builder
To start working with Table Builder, you need to create instance class of \Helpers\TableBuilder. Preferably for performance, create your instance class in any model and pass it \Helpers\Database instance to avoid duplicating database connection.
private $tableBuilder;
// Declaration of constructor in new model
public function __construct ()
{
parent::__construct();
// Example of reusing database and avoiding duplicating of database connection
$this->tableBuilder = new TableBuilder($this->db);
}
After initiating new table builder instance you can work with it.
WARNING: Table builder automatically creates a id field of type INT(11) with AUTO_INCREMENT and sets is PRIMARY KEY. If you want to set your own name or don't want to have id field, pass false as second parameter.
Creating simple table
Now we can create simple table, let's create table for comments:
// Another model's instance
public function createCommentTable ()
{
// First argument is field name and second is type or alia
$this->tableBuilder->addField('author', 'VARCHAR(40)');
$this->tableBuilder->addField('message', 'TEXT');
$this->tableBuilder->setName('comments');
$this->tableBuilder->create();
}
This example of code would create table named comments with id, author and message fields. If you would try to run this code again you'll see error. To prevent that let's set IF NOT EXISTS to true:
// First argument is field name and second is type or alia
$this->tableBuilder->addField('author', 'VARCHAR(40)');
$this->tableBuilder->addField('message', 'TEXT');
$this->tableBuilder->setName('comments');
$this->tableBuilder->setNotExists(TRUE);
$this->tableBuilder->create();
Now your code shouldn't show any errors.
Aliases
Table builder supports aliases instead of using SQL types in addField method. There's only 3 included types: int INT(11), string VARCHAR(255) and description TINYTEXT.
You can add globally your own alias, for example, in config:
// configs above
TableBuilder::setAlias('name', 'VARCHAR(40)');
// configs below
Methods
addField
Method addField is used to create field in query:
$tableBuilder->addField($field_name, $type_or_alias, $is_null, $options);
field_name is your name for the field, type_or_alias is defined type in MySQL or an alias defined in tableBuilder, is_null is by default is FALSE (so it's not null) but you can set it to TRUE if you needed and options are additional options such as AUTO_INCREMENT or CURRENT_TIMESTAMP.
Example of setting field date with CURRENT_TIMESTAMP:
$tableBuilder->addField('date', 'TIMESTAMP', FALSE, \Helpers\TableBuilder::CURRENT_TIMESTAMP);
setDefault
Method setDefault is used to determine default value of field in query. There's the example:
$tableBuilder->setDefault('group_id', 0);
This example is illustrating how to set default user group_id in table.
WARNING: Don't use setDefault for timestamps, use addField with last argument \Helpers\TableBuilder::CURRENT_TIMESTAMP instead.
create
Method create is used to finish the query and create table in the database:
$table->create();
You can pass TRUE as first argument to reset the tableBuilder and then create another table reusing the same class.
reset
Method reset resets all properties in tableBuilder in order you could start constructing table from beginning. Use it if you need to add construct another table instead of creating new instance of table builder.
Debugging
If you run into some errors with table builder, you can debug SQL code by calling getSQL method:
// Some code ...
echo $this->tableBuilder->getSQL();
Helpers\TableBuilder的更多相关文章
- Config
Config Config App Auth Cache Database Languages Mail Modules Routing Session Config Settings for the ...
- ASP.NET Core 中文文档 第四章 MVC(3.6.1 )Tag Helpers 介绍
原文:Introduction to Tag Helpers 作者:Rick Anderson 翻译:刘浩杨 校对:高嵩(Jack) 什么是 Tag Helpers? Tag Helpers 提供了什 ...
- ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)
原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...
- [asp.net core]定义Tag Helpers
原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring Getting started wi ...
- [asp.net core] Tag Helpers 简介(转)
原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...
- Handlebars块级Helpers
1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...
- 在 ASP.NET MVC 中使用 HTML Helpers 的那些事
在 ASP.NET MVC 中使用 HTML Helpers 方法,可以返回得到标准的 HTML 标签,就像 <input>.<button> 或者 <img> 等 ...
- 理解ASP.NET MVC中的HTML Helpers
01 内联Html Helpers @helper listItems(string[] items) { <ol> @foreach (var item in items) { < ...
- CKEditor Html Helpers for ASP.NET MVC3 Razor/WebForms Views
一.原生方法: 在 razor 中 使用Fckeditor 编辑内容,需要引入js <script src="@Url.Content("~/fckeditor/fckedi ...
随机推荐
- cxf 动态创建客户端,局域网能正常调用服务端,外网不能访问
- python中yield用法
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...
- bzoj 1025 [SCOI2009]游戏(置换群,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1025 [题意] 给定n,问1..n在不同的置换下变回原序列需要的不同排数有多少种. [ ...
- windows下安装和配置Weka
Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...
- Java中快如闪电的线程间通讯
这个故事源自一个很简单的想法:创建一个对开发人员友好的.简单轻量的线程间通讯框架,完全不用锁.同步器.信号量.等待和通知,在Java里开发一个轻量.无锁的线程内通讯框架:并且也没有队列.消息.事件或任 ...
- Nginx的session一致性问题
session一致性memcached缓存数据库解决方案 1.安装memcached内存数据库 yum –y install memcached 可以用telnet localhost 11211 S ...
- 【转】使用JavaScriptCore在JS和OC间通信
http://www.cocoachina.com/ios/20160623/16796.html iOS 开发中,我们时不时的需要加载一些 Web 页面,一些需求使用 Web 页面来实现可以更可控, ...
- Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)
相关资料: http://blog.csdn.net/earbao/article/details/46514313 结果注意: 1.Use IdContext.IdGlobal 这两个单元2.不能使 ...
- Cannot retrieve metalink for repository: epel. Please verify its path and try again
今天在测试环境使用yum安装,遇到一个问题: Error: Cannot retrieve metalink for repository: epel. Please verify its path ...
- android WebView将新浪天气为我所用 ------>仅供娱乐
新浪天气提供了一个网页 http://w.sina.com 浏览器访问: 这效果还可以了哦,直接用webview加载出来,效果也可以了哦,不过,这不是我要的.我不希望在我写的应用里到处铺满si ...