4 years ago (2015-08-28)  Technical application |   First to comment  53 
post score 0 times, average 0.0

Execute the query

Phinx can perform execute() and query() methods for database queries. The execute() method returns the number of rows affected by the database when executing the database query, and the query() method returns an array of results.

  Note: These command runs are based on the use of the PHP Data Objects (PDO) extension, which defines a lightweight, consistent interface to access the database in PHP.Always make sure that your query is in the same execute() command as before using pdo.This is especially important when using delimiters in inserted stored procedures or triggers that do not support delimiters.

Get line

There are two ways in which phinx can get rows: the fetchRow() method will get a single row, and the fetchAll() method will return multiple rows.Both of these methods accept native SQL statements as their only parameters.

 

Processing data sheet

Table object

Table objects are one of the most useful APIs provided by Phinx.It allows you to easily manipulate database tables using PHP code.You can use the table() method to instantiate a table object to implement database migration.

Then you can use the table object to provide methods to manipulate this table:

Create a table

Creating a table using table objects is very simple. The following example creates a table to store users:

  • Add columns: addColumn() method;
  • Adding an index: addIndex() method: Create a unique indexed column using the username and email above, and finally call save() to commit the changes to the database.
  • Note: Phinx automatically creates an auto-increment column named "id" for each table as a primary key.

We can also use the table object to specify a primary key.The following code disables Phinx from automatically generating an auto-increment column for "id" as the primary key, and uses one of the two columns to create a primary key:

  If you set a primary_key but do not use the AUTO_INCREMENT auto-increment attribute, you need to specify an id to override the default id field name:

 

Valid column data types

The data type of the column attribute is specified as a string and can be one of the following types:

  • Biginteger
  • Binary
  • Boolean
  • Date
  • Datetime
  • Decimal
  • Float
  • Integer
  • String
  • Text
  • Time
  • Timestamp
  • Uuid

In addition, MySQL also supports enum and set column types, Postgres supports json and jsonb column types (PostgreSQL 9.3 and above).

Determine if there is a table: hasTable()

 

Delete table

Use the dropTable() method, and you'd better write the down() method to re-create the table.

 

Rename table

Renaming a table is done by accessing an instance of the table object and then calling the rename() method.

 

Use columns

Get a list of columns: getColumns() method

To retrieve all table columns, you can simply create a table object and then call the getColumns() method, which will return an array.Examples are as follows:

 

Check if there is a column: hasColumn() method

You can use the hasColumn() method to check if a column exists.

 

Rename a column: renameColumn()

Access an instance of a table object and then call the renameColumn() method.

 

Add columns sequentially

To add a new column after a column, use the after option.

 

Delete a column: removeColumn() method

 

Set column attribute limits

You can use the limit option to limit the maximum length of the column:

 

Use index

Add an index table You can simply call the addIndex() method of the table object.

  By default Phinx will make the database adapter create a normal index.We can also specify a unique index with an additional parameter, the addIndex() method.

  Deleting multiple indexes calls the removeIndex() method, which you must call for each index.

  Please note: When using method removeIndex(), save() does not need to be called. The index will be deleted immediately after the method is executed.

Operation foreign key

Phinx supports creating foreign keys to constrain your database tables.The following is an example of adding a foreign key

  An array of Delete and Update options defined by the Delete and Update actions.Possible values ​​"SET_NULL", "NO_ACTION", "cascade" and "limit". We can also easily check for the presence of a foreign key:

  Finally remove the foreign key using the dropForeignKey method.

 

 

This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/3-phinx-database-query-phinx-tutorial-2.html

Post comment

Style

No Comment

登录

Forget password?

您也可以使用第三方帐号快捷登录

切换登录

注册

TW