dev-master
9999999-dev http://attwframework.github.ioDatabase component of AttwFramework
MIT
The Requires
- php >=5.3.0
framework-component
 Wallogit.com
                    
                    2017 © Pedro Peláez
                         Wallogit.com
                    
                    2017 © Pedro Peláez
                    
                    
                    
                    
                
                
            
Database component of AttwFramework
Database component of AttwFramework., (*2)
{
    "require": {
        "attwframework/db": "dev-master"
    }
}
Create an object to be the connector, passing the instance of connector configurations in the constructor, (*3)
use Attw\Db\Connection\PDOConnector;
$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
Connections collection, (*4)
use Attw\Db\Connection\Connector\Config\MySQLConnectionConfig;
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Collection as DBCollection;
$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$connections = DBCollection::getInstance();
$connections->add('ConnectionName', $connector);
The default methods of a connector are:
* Attw\Db\Connection\ConnectorInterface::getConnection() Returns the connection
* Attw\Db\Connection\ConnectorInterface::getDriver() Returns the connected driver
* Attw\Db\Connection\ConnectorInterface::query($sql) Execute a SQL query
* Attw\Db\Connection\ConnectorInterface::prepare($sql) Prepares a statement for execution and returns a statement object
* Attw\Db\Connection\ConnectorInterface::exec($sql) Execute an SQL statement and return the number of affected rows
* Attw\Db\Connection\ConnectorInterface::lastInsertId([ string $name ]) Returns the ID of the last inserted row or sequence value
* Attw\Db\Connection\ConnectorInterface::getStatement( $sql ) Returns statement class, (*5)
Methods useds for interaction with database, (*6)
Crud:, (*7)
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;
$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$storage->create('users', array(
    'name' => 'Gabriel Jacinto', 
    'email' => 'gamjj74@hotmail.com',
    'age' => 15,
    'gender' => 'male'
))->execute();
$storage->update('users', array('name' => 'Gabriel Jacinto'), array('id' => 17))
    ->execute();
$storage->remove('users', array('id' => 17))
    ->execute();
$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
print_r($stmt->fetch());
$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
$total = $stmt->rowCount();
$connector->query("DELETE FROM `users` WHERE `id` = '20'");
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;
$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$stmt = $connector->getStatement("SELECT * FROM `users` WHERE `id` = ?");
$stmt->execute(array(20));
$userData = $stmt->fetch();
Entities are classes that represent tables.
To create an entity, create a class that extends Attw\Db\Storage\Entity\AbstractEntity.
The configurations of an entity must be on a property called $_configs as an array.
If a column of the table represents a registry of other table, create an index entities like the example.
And if a column represents a datetime column, create an index datetime:, (*8)
namespace Your\Namespace\Entity;
use Attw\Db\Storage\Entity\AbstractEntity;
class User extends AbstractEntity
{
    protected $_configs = array(
        'table' => 'users',
        'primary_key' => 'id',
        'entities' => array(
            'category' => 'Your\Namespace\Entity\Category'
        ),
        'datetime' => array(
            'created_at',
            'updated_at'
        )
    );
    protected $id;
    protected $username;
    protected $email;
    protected $category;
    protected $created_at;
    protected $updated_at;
}
If you indicate the primary key and it exists, an update will be make., (*9)
Insert, (*10)
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;
use Attw\Db\Entity\EntityStorage;
$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$entityStorage = new EntityStorage($storage);
$user = new User();
$user->username = 'Gabriel';
$user->email = 'gamjj74@hotmail.com';
$entityStorage->persist($user);
Update, (*11)
$user = new User(17);//Id on contructor $user->email = 'other@email.com'; $entityStorage->persist($user);
$user = new User(17); $entityStorage->remove($user);
Fetch one, (*12)
$user = new User(21); $data = $entityStorage->fetch($user);
Fetch all, (*13)
$user = new User(); $data = $entityStorage->fetchAll($user);
Database component of AttwFramework
MIT
framework-component