MySQLのデータを配列の様に扱えるPHPのORM
PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Tank
.gitignore
LICENSE
README.md
Tank.php

README.md

Arrmy

MySQLのデータを配列の様に扱えるPHPのORM

# expample table
CREATE TABLE `table` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `data` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

How to use.

<?php
require_once 'Arrmy' . DIRECTORY_SEPARATOR . 'Tank.php';
use Arrmy\Tank;

$tank = new Tank('localhost', 'user', 'password', 'scheme');
$tank->target('table');

DML

SELECT

// SELECT * FROM `table` WHERE `id` = 123;
$result = $tank[123];

// SELECT * FROM `table` WHERE `data` LIKE 'abc';
$result = $tank[array('data' => 'abc')];

// SELECT * FROM `table` WHERE `id` = 123 AND `data` LIKE 'abc';
$result = $tank[array('id' => 123, 'data' => 'abc')];

INSERT

// REPLACE INTO `table` (`data`) VALUES ('abc');
$tank[] = array('data' => 'abc');

// REPLACE INTO `table` (`id`, `data`) VALUES (123, 'abc');
$tank[] = array('id' => 123, 'data' => 'abc');

UPDATE

// UPDATE `table` SET `data` = 'def' WHERE `id` => 123;
$tank[123] = array('data' => 'def');

// UPDATE `table` SET `data` = 'def' WHERE `id` => 123 AND `data` => `abc`;
$tank[array('id' => 123, 'data' => 'abc')] = array('data' => 'def');

DELETE

// DELETE FROM `table` WHERE 'id' => 123;
unset($tank[123]);

// DELETE FROM `table` WHERE 'id' => 123;
$tank[123] = null;

// DELETE FROM `table` WHERE `id` => 123 AND `data` => `abc`
unset($tank[array('id' => 123, 'data' => 'abc')]);

get last insert id.

$insertId = $tank['insert_id'];

get affected rows.

$affectedRows = $tank['affected_rows'];

use transaction.

$tank->begin();
try
{
    $tank[] = array('data' => 'abc');
}
catch(\Exception $e)
{
    echo $e->getMessage() . "\n";
    $tank->rollback();
}
$tank->commit();

execute SQL.

$result = $tank->chage('SELECT * FROM `table`')->fire()->fetchAll();

// prepared statement
$query = 'UPDATE `table` SET `data` = \'?\' WHERE `id` => ?';
$params = array('def', 123);
$tank->charge($query, $params)->fire();

get records.

// get all records.
foreach($tank as $key => $row)
{
    var_dump($row);
}

// get selected records.
$tank->charge('SELECT * FROM `table` WHERE id < 10')->fire();
foreach($tank as $key => $row)
{
    var_dump($row);
}

add functions.

$tank->addOperation('foo', function()
{
    $this->charge('SELECT * FROM `table` WHERE `id` = 1')->fire();
    return $this;
});
$tank->addOperation('bar', function()
{
    $this->charge('SELECT * FROM `table` WHERE `id` = 2')->fire();
    return $this;
});

foreach($tank->foo() as $key => $row)
{
    var_dump($row);
}
foreach($tank->bar() as $key => $row)
{
    var_dump($row);
}