Wikimedia SlimApp
Common classes to help with creating an application using the Slim micro framework and Twig template engine.
Loading...
Searching...
No Matches
Wikimedia\Slimapp\Dao\AbstractDao Class Reference

Base class for data access objects. More...

Public Member Functions

 __construct ( $dsn, $user, $pass, $logger=null)
 

Protected Member Functions

 transactionStart ()
 Start a new transaction.
 
 transactionCommit ()
 Commit a transaction.
 
 transactionRollback ()
 Rollback a transaction.
 
 bind ( $stmt, $values)
 Bind values to a prepared statement.
 
 fetch ( $sql, $params=null)
 Prepare and execute an SQL statement and return the first row of results.
 
 fetchAll ( $sql, $params=null)
 Prepare and execute an SQL statement and return all results.
 
 fetchAllWithFound ( $sql, $params=null)
 Prepare and execute an SQL statement and return all results plus the number of rows found on the server side.
 
 update ( $sql, $params=null)
 Prepare and execute an SQL statement in a transaction.
 
 insert ( $sql, $params=null)
 Prepare and execute an SQL statement in a transaction.
 

Static Protected Member Functions

static buildWhere (array $where, $conjunction='AND')
 Construct a where clause.
 
static buildHaving (array $having, $conjunction='AND')
 Construct a having clause.
 
static buildBooleanClause ( $type, array $expressions, $conjunction='AND')
 Construct a boolean clause.
 
static concat ()
 Create a string by joining all arguments with spaces.
 
static makeBindParams (array $list)
 Create a list of bind parameters from a list of strings.
 

Protected Attributes

 $dbh
 
 $logger
 
 $transactionCounter = 0
 

Detailed Description

Base class for data access objects.

Author
Bryan Davis bd808.nosp@m.@wik.nosp@m.imedi.nosp@m.a.or.nosp@m.g

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Slimapp\Dao\AbstractDao::__construct ( $dsn,
$user,
$pass,
$logger = null )
Parameters
string$dsnPDO data source name
string$userDatabase user
string$passDatabase password
LoggerInterface$loggerLog channel

Member Function Documentation

◆ bind()

Wikimedia\Slimapp\Dao\AbstractDao::bind ( $stmt,
$values )
protected

Bind values to a prepared statement.

If an associative array of values is provided, the data type to use when binding will be inferred by looking for a "<type>_" prefix at the beginning of the array key. This can come in very handy if you are using parameters in places like LIMIT clauses where binding as a string (the default type for PDO binds) will cause a syntax error.

Parameters
PDOStatement$stmtPreviously prepared statement
array$valuesValues to bind

◆ buildBooleanClause()

static Wikimedia\Slimapp\Dao\AbstractDao::buildBooleanClause ( $type,
array $expressions,
$conjunction = 'AND' )
staticprotected

Construct a boolean clause.

Parameters
string$typeClause type (eg 'WHERE', 'HAVING')
array$expressionsList of expressions
string$conjunctionJoining operation ('AND' or 'OR')
Returns
string Clause or empty string

◆ buildHaving()

static Wikimedia\Slimapp\Dao\AbstractDao::buildHaving ( array $having,
$conjunction = 'AND' )
staticprotected

Construct a having clause.

Parameters
array$havingList of conditions
string$conjunctionJoining operation ('and' or 'or')
Returns
string Having clause or empty string

◆ buildWhere()

static Wikimedia\Slimapp\Dao\AbstractDao::buildWhere ( array $where,
$conjunction = 'AND' )
staticprotected

Construct a where clause.

Parameters
array$whereList of conditions
string$conjunctionJoining operation ('and' or 'or')
Returns
string Where clause or empty string

◆ concat()

static Wikimedia\Slimapp\Dao\AbstractDao::concat ( )
staticprotected

Create a string by joining all arguments with spaces.

If one or more of the arguments are arrays each element of the array will be included independently.

Returns
string New string

◆ fetch()

Wikimedia\Slimapp\Dao\AbstractDao::fetch ( $sql,
$params = null )
protected

Prepare and execute an SQL statement and return the first row of results.

Parameters
string$sqlSQL
array$paramsPrepared statement parameters
Returns
array First response row

◆ fetchAll()

Wikimedia\Slimapp\Dao\AbstractDao::fetchAll ( $sql,
$params = null )
protected

Prepare and execute an SQL statement and return all results.

Parameters
string$sqlSQL
array$paramsPrepared statement parameters
Returns
array Result rows

◆ fetchAllWithFound()

Wikimedia\Slimapp\Dao\AbstractDao::fetchAllWithFound ( $sql,
$params = null )
protected

Prepare and execute an SQL statement and return all results plus the number of rows found on the server side.

The SQL is expected to contain the "SQL_CALC_FOUND_ROWS" option in the select statement. If it does not, the number of found rows returned is dependent on MySQL's interpretation of the query.

Parameters
string$sqlSQL
array$paramsPrepared statement parameters
Returns
stdClass StdClass with rows and found members

◆ insert()

Wikimedia\Slimapp\Dao\AbstractDao::insert ( $sql,
$params = null )
protected

Prepare and execute an SQL statement in a transaction.

Parameters
string$sqlSQL
array | null$paramsPrepared statement parameters
Returns
string|false Last insert id or false if an exception was generated

◆ makeBindParams()

static Wikimedia\Slimapp\Dao\AbstractDao::makeBindParams ( array $list)
staticprotected

Create a list of bind parameters from a list of strings.

Parameters
array$listList of strings to convert to bind parameters
Returns
array List of bind parameters (eg ':field1)

◆ transactionCommit()

Wikimedia\Slimapp\Dao\AbstractDao::transactionCommit ( )
protected

Commit a transaction.

If the transaction counter is zero, commit the transaction otherwise decrement the transaction counter. This method is useful in nested transactions.

Returns
bool True on success, false on failure.

◆ transactionRollback()

Wikimedia\Slimapp\Dao\AbstractDao::transactionRollback ( )
protected

Rollback a transaction.

If the transaction counter is greater than 0, set it to 0 and rollback the transaction. This method is useful in nested transactions.

Returns
bool True on success, false on failure.

◆ transactionStart()

Wikimedia\Slimapp\Dao\AbstractDao::transactionStart ( )
protected

Start a new transaction.

If already a transaction has been started, it will only increment the counter. This method is useful in nested transactions.

Returns
bool True on success, false on failure.

◆ update()

Wikimedia\Slimapp\Dao\AbstractDao::update ( $sql,
$params = null )
protected

Prepare and execute an SQL statement in a transaction.

Parameters
string$sqlSQL
array$paramsPrepared statement parameters
Returns
bool False if an exception was generated, true otherwise

The documentation for this class was generated from the following file: