Show:

Db_Mysql Class

Extends Db_Interface
Module: Db

This class lets you create and use PDO database connections.

Constructor

Db_Mysql

(
  • $connectionName
  • [$pdo=null]
)

Parameters:

  • $connectionName String

    The name of the connection out of the connections added with Db::setConnection() This is required for actually connecting to the database.

  • [$pdo=null] PDO optional

    Existing PDO connection. Only accepts connections to MySQL.

Methods

__call

(
  • $name
  • $arguments
)
Mixed

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:201

Forwards all other calls to the PDO object

Parameters:

  • $name String

    The function name

  • $arguments Array

    The arguments

Returns:

Mixed:

The result of method call

codeForModelBaseClass

(
  • $table
  • $directory
  • [$classname_prefix='']
  • [$class_name_base=null]
  • [$prefix=null]
  • [$js_code=null]
  • [$table_comment='']
)
String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:1528

Generates code for a base class for the model

Parameters:

  • $table String

    The name of the table to generate the code for.

  • $directory String

    The path of the directory in which to place the model code.

  • [$classname_prefix=''] String optional

    The prefix to prepend to the generated class names

  • [$class_name_base=null] &string optional

    If set, this is the class name that is used. If an unset variable is passed, it is filled with the class name that is ultimately chosen, without the $classname_prefix

  • [$prefix=null] String optional

    Defaults to the prefix of the tables, as specified in the connection. Pass null here to use the default, or a string to override it.

  • [$js_code=null] &string optional

    The javascript code for the base class

  • [$table_comment=''] &string optional

    The comment from the MySQL table if any

Returns:

String:

The generated code for the class.

connection

() String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:227

Returns the connection info with which this Db object was created.

Returns:

String:

connectionName

() String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:217

Returns the name of the connection with which this Db object was created.

Returns:

String:

dbms

() String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:257

Returns the lowercase name of the dbms (e.g. "mysql")

Returns:

String:

dbName

() String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:267

Returns the name of the database used

Returns:

String:

delete

(
  • $table_from
  • [$table_using=null]
)
Db_Query_Mysql

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:499

Creates a query to delete rows.

Parameters:

  • $table_from String

    The table to delete from

  • [$table_using=null] String optional

    If set, adds a USING clause with this table. You can then use ->join() with the resulting Db_Query.

Returns:

dsn

() Array

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:240

Returns an associative array representing the dsn

Returns:

Array:

fromDate

(
  • $datetime
)
Integer

Returns a timestamp from a Date string

Parameters:

  • $datetime String

    The Date string that comes from the db

Returns:

Integer:

The timestamp

fromDateTime

(
  • $datetime
)
Integer

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:717

Returns a timestamp from a DateTime string

Parameters:

  • $datetime String

    The DateTime string that comes from the db

Returns:

Integer:

The timestamp

generateModels

(
  • $directory
  • [$classname_prefix=null]
)
Array

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:1091

Generates base classes of the models, and if they don't exist, skeleton code for the models themselves. Use it only after you have made changes to the database schema. You shouldn't be using it on every request.

Parameters:

  • $directory String

    The directory in which to generate the files. If the files already exist, they are not overwritten, unless they are inside the "Base" subdirectory. If the "Base" subdirectory does not exist, it is created.

  • [$classname_prefix=null] String optional

    The prefix to prepend to the Base class names. If not specified, prefix becomes "connectionName_", where connectionName is the name of the connection.

Returns:

Array:

$filenames The array of filenames for files that were saved.

insert

(
  • $table_into
  • $fields=array()
)
Db_Query_Mysql

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:297

Creates a query to insert a row into a table

Parameters:

  • $table_into String

    The name of the table to insert into

  • $fields=array() Array

    The fields as an array of column=>value pairs

Returns:

Db_Query_Mysql:

The resulting Db_Query_Mysql object

insertManyAndExecute

(
  • $table_into
  • [$rows=array()]
  • [$options=array()]
)

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:332

Inserts multiple rows into a single table, preparing the statement only once, and executes all the queries.

Parameters:

  • $table_into String

    The name of the table to insert into

  • [$rows=array()] Array optional

    The array of rows to insert. Each row should be an array of ($field => $value) pairs, with the exact same set of keys (field names) in each array. It can also be a Db_Row.

  • [$options=array()] Array optional

    An associative array of options, including:

    • [className] String optional

      If you provide the class name, the system will be able to use any sharding indexes under that class name in the config.

    • [chunkSize] Integer optional

      The number of rows to insert at a time. Defaults to 20. You can also put 0 here, which means unlimited chunks, but it's not recommended.

    • [onDuplicateKeyUpdate] Array optional

      You can put an array of fieldname => value pairs here, which will add an ON DUPLICATE KEY UPDATE clause to the query.

rank

(
  • $table
  • $pts_field
  • $rank_field
  • [$start=1]
  • [$chunk_size=1000]
  • [$rank_level2=0]
  • [$order_by]
  • [$where=null]
)

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:554

Sorts a table in chunks

Parameters:

  • $table String

    The name of the table in the database

  • $pts_field String

    The name of the field to rank by.

  • $rank_field String

    The rank field to update in all the rows

  • [$start=1] Integer optional

    The value of the first rank

  • [$chunk_size=1000] Integer optional

    The number of rows to process at a time. Default is 1000. This is so the queries don't tie up the database server for very long, letting it service website requests and other things.

  • [$rank_level2=0] Integer optional

    Since the ranking is done in chunks, the function must know which rows have not been processed yet. If this field is empty (default) then the function sets the rank_field to 0 in all the rows, before starting the ranking process. (That might be a time consuming operation.) Otherwise, if $rank is a nonzero integer, then the function alternates between the ranges $start to $rank_level2, and $rank_level2 + $start to $rank_level2 * 2. That is, after it is finished, all the ratings will be in one of these two ranges. If not empty, this should be a very large number, like a billion.

  • [$order_by] Array optional

    The order clause to use when calculating ranks. Default is array($pts_field, false)

  • [$where=null] Array optional

    Any additional criteria to filter the table by. The ranking algorithm will do its work within the results that match this criteria. If your table is sharded, then all the work must be done within one shard.

rawQuery

(
  • $sql
  • [$bind=array()]
)
Db_Query_Mysql

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:522

Creates a query from raw SQL

Parameters:

  • $sql String | Null

    May contain one or more SQL statements. Pass null here for an empty query that you can add other clauses to, e.g. ->commit().

  • [$bind=array()] Array optional

    An array of parameters to bind to the query, using the Db_Query_Mysql->bind method.

Returns:

reallyConnect

(
  • [$shardName=null]
)
PDO

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:76

Actually makes a connection to the database (by creating a PDO instance)

Parameters:

  • [$shardName=null] Array optional

    A shard name that was added using Db::setShard. This modifies how we connect to the database.

Returns:

PDO:

The PDO object for connection

scriptToQueries

(
  • $script
  • [$callback=null]
)
Array

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:790

Takes a MySQL script and returns an array of queries. When DELIMITER is changed, respects that too.

Parameters:

  • $script String

    The text of the script

  • [$callback=null] Callable optional

    Optional callback to call for each query.

Returns:

Array:

An array of the SQL queries.

scriptToQueries_internal

(
  • $script
  • [$callback=null]
)
Array protected

Takes stripped MySQL script and returns an array of queries. When DELIMITER is changed, respects that too.

Parameters:

  • $script String

    The text of the script

  • [$callback=null] Callable optional

    Optional callback to call for each query.

Returns:

Array:

An array of the SQL queries.

select

(
  • [$fields='*']
  • [$tables='']
)
Db_Query_Mysql

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:280

Creates a query to select fields from a table. Needs to be used with Db_Query::from().

Parameters:

  • [$fields='*'] String | Array optional

    The fields as strings, or "*", or array of alias=>field

  • [$tables=''] String | Array optional

    The tables as strings, or array of alias=>table

Returns:

Db_Query_Mysql:

The resulting Db_Query object

setTimezone

(
  • [$offset=timezone_offset_get()]
)

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:171

Sets the timezone in the database to match the one in PHP

Parameters:

  • [$offset=timezone_offset_get()] Integer optional

    in seconds

shardName

() String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:191

Returns the name of the shard currently selected with reallyConnect, if any

Returns:

String:

timestamp

() Integer

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:772

Returns the timestamp the db server would have, based on synchronization

Returns:

Integer:

toDate

(
  • $timestamp
)
String

Returns a Date string to store in the database

Parameters:

  • $timestamp String

    The UNIX timestamp, e.g. from a strtotime function

Returns:

String:

toDateTime

(
  • $timestamp
)
String

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:755

Returns a DateTime string to store in the database

Parameters:

  • $timestamp String

    The UNIX timestamp, e.g. from a strtotime function

Returns:

String:

uniqueId

(
  • $table
  • $field
  • [$where=array()]
  • [$options=array()]
)

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:651

Generate an ID that is unique in a table

Parameters:

  • $table String

    The name of the table

  • $field String

    The name of the field to check for uniqueness. You should probably have an index starting with this field.

  • [$where=array()] Array optional

    You can indicate conditions here to limit the search for an existing value. The result is an id that is unique within a certain partition.

  • [$options=array()] Array optional

    Optional array used to override default options:

    • [length=8] Integer optional

      The length of the ID to generate, after the prefix.

    • [characters='abcdefghijklmnopqrstuvwxyz'] String optional

      All the characters from which to construct the id

    • [prefix=''] String optional

      The prefix to prepend to the unique id.

    • [filter] Callable optional

      The name of a function that will take the generated string and check it. The filter function can modify the string by returning another string, or simply reject the string by returning false, in which another string will be

update

(
  • $table
)
Db_Query_Mysql

Inherited from Db_Interface but overwritten in platform/classes/Db/Mysql.php:484

Creates a query to update rows. Needs to be used with {@link Db_Query::set}

Parameters:

  • $table String

    The table to update

Returns:

Db_Query_Mysql:

The resulting Db_Query object

update

(
  • $criteria
)
Db_Query_Mysql

Creates a query to rollback a previously started transaction.

Parameters:

  • $criteria Array

    The criteria to use, for sharding

Returns:

Db_Query_Mysql:

The resulting Db_Query object

Properties

$connectionName

String protected

The name of the connection

$dbname

String

The database name of the shard currently selected with reallyConnect, if any

$maxCheckStrlen

String

The cutoff after which strlen gets too expensive to check automatically

$pdo

PDO

The PDO connection that this object uses

$prefix

String

The prefix of the shard currently selected with reallyConnect, if any

$shardName

String protected

The name of the shard currently selected with reallyConnect, if any

Events

Db/reallyConnect

Before

Occurs before a real connection to the database is made

Event Payload:

  • db Db_Mysql
  • shardName String
  • modifications Array

Db/reallyConnect

After

Occurs when a real connection to the database has been made

Event Payload:

  • db Db_Mysql
  • shardName String
  • modifications Array