Repository navigation
Anton edited this page Oct 19, 2013
·
22 revisions
Удобная обертка для PDO
<?php
// configuration example
"db" => array(
"connect" => array(
"type" => "mysql",
"host" => "localhost",
"name" => "bluz",
"user" => "root",
"pass" => "",
"options" => array(
\PDO::ATTR_PERSISTENT => true
)
),
"attributes" => array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
),
"defaultAdapter" => true
),
<?php
// init settings, not connect!
$db->setConnect(array(
'type' => 'mysql',
'host' => 'localhost',
'name' => 'db name',
'user' => 'root',
'pass' => ''
));
<?php
// quote variable
$db->quote($id)
// quote column name
$db->quoteIdentifier($column)
<?php
// query
$db->query("SET NAMES 'utf8'");
<?php
// get one
$db->fetchOne("SELECT COUNT(*) FROM users");
// get row array
$db->fetchRow("SELECT * FROM users WHERE id = ". $db->quote($id));
$db->fetchRow("SELECT * FROM users WHERE id = ?", array($id));
$db->fetchRow("SELECT * FROM users WHERE id = :id", array(':id'=>$id));
// get column
$db->fetchColumn("SELECT id FROM users WHERE ip = :ip", array(':ip'=>'127.0.0.1'));
// get array
$db->fetchAll("SELECT * FROM users WHERE ip = ?", array($ip));
// get pairs
$db->fetchPairs("SELECT ip, COUNT(id) FROM users GROUP BY ip", array());
// get group
$db->fetchGroup("SELECT ip, id FROM users");
<?php
// get object
// .. to stdClass
$stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id));
// .. to new Some object
$someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id), 'Some');
// .. to exists instance of Some object
$someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id), $someClass);
<?php
// controller code
if ($this->getDb()->transaction(function() {
/**
* @var \Bluz\Application $this
*/
$this->getDb()->delete('acl_privileges');
})) {
$this->getMessages()->addSuccess('All data was saved');
} else {
$this->getMessages()->addError('Internal server error');
}
Acl
Application
Auth
Cache
Common
— Exception
— Collection
— Container
— Helper
— Options
— Singleton
Config
Controller
— Data
— Mapper
—— Crud
—— Rest
— Reflection
Crud
— Crud Table
Db
— Row
— Table
— Relations
— Query
Debug
EventManager
Grid
Http
Layout
Logger
Mailer
Messages
Nil
Proxy
Registry
Request
Response
Router
Session
Translator
Validator
View