PHP Class ManaPHP\Db

Inheritance: extends Component, implements manaphp\DbInterface
Afficher le fichier Open project: manaphp/manaphp Class Usage Examples

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode 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 méthode

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

_executePrepared() protected méthode

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
Résultat PDOStatement

_parseBindValue() protected méthode

protected _parseBindValue ( mixed $value, integer $preservedStrLength ) : integer | string
$value mixed
$preservedStrLength integer
Résultat integer | string

affectedRows() public méthode

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

begin() public méthode

Starts a transaction in the connection
public begin ( ) : void
Résultat void

commit() public méthode

Commits the active transaction in the connection
public commit ( ) : void
Résultat void

delete() public méthode

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
Résultat integer

execute() public méthode

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
Résultat integer

fetchAll() public méthode

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
Résultat array

fetchOne() public méthode

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
Résultat array | false

getBind() public méthode

Active SQL statement in the object
public getBind ( ) : array
Résultat array

getEmulatedSQL() public méthode

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

getSQL() public méthode

Active SQL statement in the object
public getSQL ( ) : string
Résultat string

insert() public méthode

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
Résultat void

isUnderTransaction() public méthode

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

lastInsertId() public méthode

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

limit() public méthode

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
Résultat string

query() public méthode

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
Résultat PdoStatement

rollback() public méthode

Rollbacks the active transaction in the connection
public rollback ( ) : void
Résultat void

update() public méthode

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
Résultat integer

Property Details

$_affectedRows protected_oe property

Last affected rows
protected int $_affectedRows
Résultat integer

$_bind protected_oe property

Active SQL bound parameter variables
protected array $_bind
Résultat array

$_dsn protected_oe property

protected string $_dsn
Résultat string

$_options protected_oe property

protected array $_options
Résultat array

$_password protected_oe property

protected string $_password
Résultat string

$_pdo protected_oe property

protected PDO $_pdo
Résultat PDO

$_sql protected_oe property

Active SQL Statement
protected string $_sql
Résultat string

$_transactionLevel protected_oe property

Current transaction level
protected int $_transactionLevel
Résultat integer

$_type protected_oe property

Type of database system driver is used for
protected string $_type
Résultat string

$_username protected_oe property

protected string $_username
Résultat string