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 ...
随机推荐
- Ubuntu 12.04中文输入法的安装
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu ...
- 【跟我一起学Python吧】Python的包管理工具
刚开始学习Python时,在看文档和别人的blog介绍安装包有的用easy_install, setuptools, 有的使用pip,distribute,那麽这几个工具有什么关系呢,看一下下面这个图 ...
- 【JAVA版】Storm程序整合Kafka、Mongodb示例及部署
一.环境 一台Centos6.5主机 Mongo 3.0 Kafka_2.11-0.8.2.1 Storm-0.9.5 Zookeeper-3.4.6 java 1.7 (后因在mac上打包的jar由 ...
- 有趣的Node爬虫,数据导出成Excel
最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...
- rsync 无密码传输文件
最近机器迁移,需要备份文件,但各个机器间不能穿梭,即无法通过scp来传输文件, 在运维的建议下,选用了rsync作为传输的工具. 默认情况Ubuntu安装了rsync服务,但在/etc下没有配置文件, ...
- [iOS基础控件 - 7.0] UIWebView
A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的 2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...
- [iOS基础控件 - 6.11.2] - UINavigationController 多控制器 简单使用
A.概念 1.通常一个app有多个控制器 2.需要对这些控制器进行管理 3.有多个view的时候,用一个父view去管理多个子view 4.控制器管理也是如此,使用给一个父控制器,去控制子控制器 ...
- CAAnimation解读
序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...
- << 移位运算
/** * The maximum capacity, used if a higher value is implicitly specified * by either of the constr ...
- baseadapter.getItemId的使用方法:实现listview筛选、动态删除
转载:http://www.lai18.com/content/1631131.html 这里的listview筛选是指listview的adapter实现filter来过滤数据. “动态删除&quo ...