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 ...
随机推荐
- InputFormat,OutputFormat,InputSplit,RecordRead(一些常见面试题),使用yum安装64位Mysql
列举出hadoop常用的一些InputFormat InputFormat是用来对我们的输入数据进行格式化的.TextInputFormat是默认的. InputFormat有哪些类型? DBInpu ...
- 软件开发杂谈之从需求到上线---valen
背景 IT已经成为当代企业必不可少的竞争手段,从无到有到标配,可以说以后不懂IT的就是文盲这句一点也不过,而软件开发是个复杂工程,零零碎碎各种理论工具和技巧,一言难尽. 本文意在言简意赅,简述软件开发 ...
- as3 中trace() 函数对效率的影响
进行页游开发的过程中,很多开发者都有一个习惯,在数据输出中添加trace()函数来跟踪数值 - 不进行条件编译,发布的时候也不删除.实际上大量的trace函数会降低程序的效率,我们可以用一个简单的例子 ...
- MANACHER---求最长回文串
求最长回文串,如果是暴力的方法的话,会枚举每个字符为中心,然后向两边检测求出最长的回文串,时间复杂度在最坏的情况下就是0(n^2),为什么时间复杂度会这么高,因为对于每一个作为中心的字符的检测是独立的 ...
- Spring MVC MultiActionController example
In Spring MVC application, MultiActionController is used to group related actions into a single cont ...
- AutoCAD.NET二次开发:创建自定义菜单(COM)
当我们要在CAD中创建自定菜单时,可以引用COM组件来实现. 下面是实现方式: 1.新建类库项目,并引用CAD目录(我这里用的是CAD2008)下的acdbmgd.dll.acmgd.dll,并将引用 ...
- UIImage 相关操作
修改UIImage大小 修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形.目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageV ...
- ElasticSearch中文分词(IK)
ElasticSearch常用的很受欢迎的是IK,这里稍微介绍下安装过程及测试过程. 1.ElasticSearch官方分词 自带的中文分词器很弱,可以体检下: [zsz@VS-zsz ~]$ c ...
- sql:[dbo].[smt_MES_RptProductDaily] 生产日报表
USE [ChangHongMES_904]GO/****** Object: StoredProcedure [dbo].[smt_MES_RptProductDaily] Script Date: ...
- Linux chmod命令修改文件与文件夹权限命令代码
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限. 以下转自:http://www.codeceo.com/article/linux-chmod-co ...