PHP Class ManaPHP\Db

Inheritance: extends Component, implements manaphp\DbInterface
Show file Open project: manaphp/manaphp Class Usage Examples

Protected Properties

Property Type Description
$_affectedRows integer Last affected rows
$_bind array Active SQL bound parameter variables
$_dsn string
$_options array
$_password string
$_pdo PDO
$_sql string Active SQL Statement
$_transactionLevel integer Current transaction level
$_type string Type of database system driver is used for
$_username string

Public Methods

Method Description
__construct ( array $options ) \ManaPHP\Db\Adapter constructor
affectedRows ( ) : integer Returns the number of affected rows by the last INSERT/UPDATE/DELETE reported by the database system
begin ( ) : void Starts a transaction in the connection
commit ( ) : void Commits the active transaction in the connection
delete ( string $table, string | array $conditions, array $bind = [] ) : integer Deletes data from a table using custom SQL syntax Deleting existing robot $success = $connection->delete( "robots", "id = 101" ); Next SQL sentence is generated DELETE FROM robots WHERE id = 101
execute ( string $sql, array $bind = [] ) : integer Sends SQL statements to the database server returning the success state.
fetchAll ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : array Dumps the complete result of a query into an array Getting all robots with associative indexes only $robots = $connection->fetchAll("SELECT * FROM robots", \ManaPHP\Db::FETCH_ASSOC); foreach ($robots as $robot) { print_r($robot); } Getting all robots that contains word "robot" withing the name $robots = $connection->fetchAll("SELECT * FROM robots WHERE name LIKE :name", ManaPHP\Db::FETCH_ASSOC, array('name' => '%robot%') ); foreach($robots as $robot){ print_r($robot); }
fetchOne ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : array | false Returns the first row in a SQL query result Getting first robot $robot = $connection->fetchOne("SELECT * FROM robots"); print_r($robot); Getting first robot with associative indexes only $robot = $connection->fetchOne("SELECT * FROM robots", \ManaPHP\Db::FETCH_ASSOC); print_r($robot);
getBind ( ) : array Active SQL statement in the object
getEmulatedSQL ( integer $preservedStrLength ) : string Active SQL statement in the object with replace the bind with value
getSQL ( ) : string Active SQL statement in the object
insert ( string $table, array $columnValues ) : void Inserts data into a table using custom SQL syntax Inserting a new robot $success = $connection->insert( "robots", array("Boy", 1952), array("name", "year") ); Next SQL sentence is sent to the database system INSERT INTO robots (name, year) VALUES ("boy", 1952);
isUnderTransaction ( ) : boolean Checks whether the connection is under a transaction $connection->begin(); var_dump($connection->isUnderTransaction()); //true
lastInsertId ( ) : integer Returns insert id for the auto_increment column inserted in the last SQL statement
limit ( string $sql, integer $number, integer $offset ) : string Appends a LIMIT clause to $sqlQuery argument echo $connection->limit("SELECT * FROM robots", 5);
query ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : PdoStatement Sends SQL statements to the database server returning the success state.
rollback ( ) : void Rollbacks the active transaction in the connection
update ( string $table, array $columnValues, string | array $conditions, array $bind = [] ) : integer Updates data on a table using custom SQL syntax Updating existing robot $success = $connection->update( "robots", array("name"), array("New Boy"), "id = 101" ); Next SQL sentence is sent to the database system UPDATE robots SET name = "boy" WHERE id = 101

Protected Methods

Method Description
_executePrepared ( PDOStatement $statement, array $bind ) : PDOStatement Executes a prepared statement binding. This function uses integer indexes starting from zero $statement = $db->prepare('SELECT * FROM robots WHERE name = :name'); $result = $connection->executePrepared($statement, array('name' => 'mana'));
_parseBindValue ( mixed $value, integer $preservedStrLength ) : integer | string

Method Details

__construct() public method

\ManaPHP\Db\Adapter constructor
public __construct ( array $options )
$options array

_executePrepared() protected method

Executes a prepared statement binding. This function uses integer indexes starting from zero $statement = $db->prepare('SELECT * FROM robots WHERE name = :name'); $result = $connection->executePrepared($statement, array('name' => 'mana'));
protected _executePrepared ( PDOStatement $statement, array $bind ) : PDOStatement
$statement PDOStatement
$bind array
return PDOStatement

_parseBindValue() protected method

protected _parseBindValue ( mixed $value, integer $preservedStrLength ) : integer | string
$value mixed
$preservedStrLength integer
return integer | string

affectedRows() public method

Returns the number of affected rows by the last INSERT/UPDATE/DELETE reported by the database system
public affectedRows ( ) : integer
return integer

begin() public method

Starts a transaction in the connection
public begin ( ) : void
return void

commit() public method

Commits the active transaction in the connection
public commit ( ) : void
return void

delete() public method

Deletes data from a table using custom SQL syntax Deleting existing robot $success = $connection->delete( "robots", "id = 101" ); Next SQL sentence is generated DELETE FROM robots WHERE id = 101
public delete ( string $table, string | array $conditions, array $bind = [] ) : integer
$table string
$conditions string | array
$bind array
return integer

execute() public method

Use this method only when the SQL statement sent to the server does n't return any rows Inserting data $success = $connection->execute("INSERT INTO robots VALUES (1, 'Boy')"); $success = $connection->execute("INSERT INTO robots VALUES (?, ?)", array(1, 'Boy'));
public execute ( string $sql, array $bind = [] ) : integer
$sql string
$bind array
return integer

fetchAll() public method

Dumps the complete result of a query into an array Getting all robots with associative indexes only $robots = $connection->fetchAll("SELECT * FROM robots", \ManaPHP\Db::FETCH_ASSOC); foreach ($robots as $robot) { print_r($robot); } Getting all robots that contains word "robot" withing the name $robots = $connection->fetchAll("SELECT * FROM robots WHERE name LIKE :name", ManaPHP\Db::FETCH_ASSOC, array('name' => '%robot%') ); foreach($robots as $robot){ print_r($robot); }
public fetchAll ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : array
$sql string
$bind array
$fetchMode integer
return array

fetchOne() public method

Returns the first row in a SQL query result Getting first robot $robot = $connection->fetchOne("SELECT * FROM robots"); print_r($robot); Getting first robot with associative indexes only $robot = $connection->fetchOne("SELECT * FROM robots", \ManaPHP\Db::FETCH_ASSOC); print_r($robot);
public fetchOne ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : array | false
$sql string
$bind array
$fetchMode integer
return array | false

getBind() public method

Active SQL statement in the object
public getBind ( ) : array
return array

getEmulatedSQL() public method

Active SQL statement in the object with replace the bind with value
public getEmulatedSQL ( integer $preservedStrLength ) : string
$preservedStrLength integer
return string

getSQL() public method

Active SQL statement in the object
public getSQL ( ) : string
return string

insert() public method

Inserts data into a table using custom SQL syntax Inserting a new robot $success = $connection->insert( "robots", array("Boy", 1952), array("name", "year") ); Next SQL sentence is sent to the database system INSERT INTO robots (name, year) VALUES ("boy", 1952);
public insert ( string $table, array $columnValues ) : void
$table string
$columnValues array
return void

isUnderTransaction() public method

Checks whether the connection is under a transaction $connection->begin(); var_dump($connection->isUnderTransaction()); //true
public isUnderTransaction ( ) : boolean
return boolean

lastInsertId() public method

Returns insert id for the auto_increment column inserted in the last SQL statement
public lastInsertId ( ) : integer
return integer

limit() public method

Appends a LIMIT clause to $sqlQuery argument echo $connection->limit("SELECT * FROM robots", 5);
public limit ( string $sql, integer $number, integer $offset ) : string
$sql string
$number integer
$offset integer
return string

query() public method

Use this method only when the SQL statement sent to the server is returning rows Querying data $resultset = $connection->query("SELECT * FROM robots WHERE type='mechanical'"); $resultset = $connection->query("SELECT * FROM robots WHERE type=?", array("mechanical"));
public query ( string $sql, array $bind = [], integer $fetchMode = PDO::FETCH_ASSOC ) : PdoStatement
$sql string
$bind array
$fetchMode integer
return PdoStatement

rollback() public method

Rollbacks the active transaction in the connection
public rollback ( ) : void
return void

update() public method

Updates data on a table using custom SQL syntax Updating existing robot $success = $connection->update( "robots", array("name"), array("New Boy"), "id = 101" ); Next SQL sentence is sent to the database system UPDATE robots SET name = "boy" WHERE id = 101
public update ( string $table, array $columnValues, string | array $conditions, array $bind = [] ) : integer
$table string
$columnValues array
$conditions string | array
$bind array
return integer

Property Details

$_affectedRows protected property

Last affected rows
protected int $_affectedRows
return integer

$_bind protected property

Active SQL bound parameter variables
protected array $_bind
return array

$_dsn protected property

protected string $_dsn
return string

$_options protected property

protected array $_options
return array

$_password protected property

protected string $_password
return string

$_pdo protected property

protected PDO $_pdo
return PDO

$_sql protected property

Active SQL Statement
protected string $_sql
return string

$_transactionLevel protected property

Current transaction level
protected int $_transactionLevel
return integer

$_type protected property

Type of database system driver is used for
protected string $_type
return string

$_username protected property

protected string $_username
return string