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 ...
随机推荐
- 单片机usb转串口的时灵时不灵的解答
写这篇博客,首先检讨一下自己,因为以前串口的程序,也和步进电机一样,时灵时不灵,我现在终于知道这是为什么了,因为51上有三个串口,一个公口,一个母口,一个usb转串口,这样的话,串口有3个了,我手头上 ...
- <Araxis Merge>快速一览文件的比较与合并
重要的文件比较与合并特性在下面都指出了.对每个特性的说明性内容在下面可以找到. 注意:只有双向的比较/合并被展示了,专业版的Merge还支持三向的比较/合并. 1.文件夹比较按钮 单击这个工具栏按钮会 ...
- 分布式应用框架Akka快速入门
转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...
- 内核参数优化/etc/sysctl.conf
net.nf_conntrack_max = 65536000net.netfilter.nf_conntrack_tcp_timeout_established = 1200net.ipv4.tcp ...
- UVALive 7276 Wooden Signs (DP)
Wooden Signs 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/E Description http://7xjob4. ...
- Map 排序
/** * 通过map 的 value 排序,并返回排序后的第一个条目 * * @param m 待排序集合 * @param desc true:降序排序,false:升序排序 * @return ...
- mysql的interval函数用法
Name: 'INTERVAL' Description: Syntax: INTERVAL(N,N1,N2,N3,...) Returns 0 if N < N1, 1 if N < N ...
- Unity3D之UGUI学习笔记(二):Rect Transform与Anchor
Rect Transform 我们都知道,Unity3D中所有的GameObject都必须要携带一个Transform组件,且该组件无法移除,那么作为UI显示的GameObject则不是携带Trans ...
- MES生产日报存储过程
USE [ScreenMonitor]GO/****** Object: StoredProcedure [dbo].[ImportProductForDay] Script Date: 04/11/ ...
- spark结合 Openfire服务器,发送聊天消息
1.下载OpenFire服务器,进行安装,参考http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html 2.程序运行客户端:下载客户端代 ...