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.

  1. private $tableBuilder;
  2. // Declaration of constructor in new model
  3. public function __construct ()
  4. {
  5. parent::__construct();
  6. // Example of reusing database and avoiding duplicating of database connection
  7. $this->tableBuilder = new TableBuilder($this->db);
  8. }

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:

  1. // Another model's instance
  2. public function createCommentTable ()
  3. {
  4. // First argument is field name and second is type or alia
  5. $this->tableBuilder->addField('author', 'VARCHAR(40)');
  6. $this->tableBuilder->addField('message', 'TEXT');
  7. $this->tableBuilder->setName('comments');
  8. $this->tableBuilder->create();
  9. }

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:

  1. // First argument is field name and second is type or alia
  2. $this->tableBuilder->addField('author', 'VARCHAR(40)');
  3. $this->tableBuilder->addField('message', 'TEXT');
  4. $this->tableBuilder->setName('comments');
  5. $this->tableBuilder->setNotExists(TRUE);
  6. $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:

  1. // configs above
  2. TableBuilder::setAlias('name', 'VARCHAR(40)');
  3. // configs below

Methods

addField

Method addField is used to create field in query:

  1. $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:

  1. $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:

  1. $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:

  1. $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:

  1. // Some code ...
  2. echo $this->tableBuilder->getSQL();

Helpers\TableBuilder的更多相关文章

  1. Config

    Config Config App Auth Cache Database Languages Mail Modules Routing Session Config Settings for the ...

  2. ASP.NET Core 中文文档 第四章 MVC(3.6.1 )Tag Helpers 介绍

    原文:Introduction to Tag Helpers 作者:Rick Anderson 翻译:刘浩杨 校对:高嵩(Jack) 什么是 Tag Helpers? Tag Helpers 提供了什 ...

  3. ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)

    原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...

  4. [asp.net core]定义Tag Helpers

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring Getting started wi ...

  5. [asp.net core] Tag Helpers 简介(转)

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...

  6. Handlebars块级Helpers

    1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...

  7. 在 ASP.NET MVC 中使用 HTML Helpers 的那些事

    在 ASP.NET MVC 中使用 HTML Helpers 方法,可以返回得到标准的 HTML 标签,就像 <input>.<button> 或者 <img> 等 ...

  8. 理解ASP.NET MVC中的HTML Helpers

    01 内联Html Helpers @helper listItems(string[] items) { <ol> @foreach (var item in items) { < ...

  9. CKEditor Html Helpers for ASP.NET MVC3 Razor/WebForms Views

    一.原生方法: 在 razor 中 使用Fckeditor 编辑内容,需要引入js <script src="@Url.Content("~/fckeditor/fckedi ...

随机推荐

  1. 一个鼠标键盘控制两台甚至多台主机的方法--Synergy

    在多台主机,不同系统中操作.避免了更换键鼠的麻烦.即使下面图中的功能. 鼠标同时在三台或者多台主机之间进行移动,而且是无缝滑动,鼠标直接从左滑倒右,而且支持,这台电脑复制,另一台黏贴.非常的方便实用. ...

  2. PLSQL 连接Oracle11g (64位)

    1.用plsql连不上oracle 11g(64位),先去下载一个oracle 11g(32位客户端) http://www.oracle.com/technetwork/database/featu ...

  3. fedora20安装spin以及用户界面ispin

    (博客园-番茄酱原创) (最近感觉用make会出现库错误,所以改进了教程,把之前的make步骤省掉了,直接下载可执行文件进行配置最简单啦...) 1.首先,下载对应版本的spin,我64位的fedor ...

  4. 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)

    1.冒泡排序: (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)外面再套个循环就行. 算法复杂度:O(N2)   不罗嗦,上代码: //冒泡排序(两两交换,外加一个外循环) pub ...

  5. Spark SQL概念学习系列之Spark SQL的简介(一)

    Spark SQL提供在大数据上的SQL查询功能,类似于Shark在整个生态系统的角色,它们可以统称为SQL on Spark. 之前,Shark的查询编译和优化器依赖于Hive,使得Shark不得不 ...

  6. linux性能问题(CPU,内存,磁盘I/O,网络)

    一. CPU性能评估 1.vmstat [-V] [-n] [depay [count]] -V : 打印出版本信息,可选参数 -n : 在周期性循环输出时,头部信息仅显示一次 delay : 两次输 ...

  7. HDU 3661 Assignments (水题,贪心)

    题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: ...

  8. HDU 4463 Outlets (最小生成树)

    题意:给定n个点坐标,并且两个点已经连接,但是其他的都没有连接,但是要找出一条最短的路走过所有的点,并且路线最短. 析:这个想仔细想想,就是应该是最小生成树,把所有两点都可以连接的当作边,然后按最小生 ...

  9. properties.load(in); 引出的中文乱码问题

    /** * Reads a property list (key and element pairs) from the input byte stream. * The input stream i ...

  10. 如何开发原生的 JavaScript 插件(知识点+写法)

    一.前言 通过 "WWW" 原则我们来了解 JavaScript 插件这个东西 第一个 W "What" -- 是什么?什么是插件,我就不照搬书本上的抽象概念了 ...