Exemplo n.º 1
0
    function testQuery() {
        $conn = new Ganc_ConnectionManager();
        $dsn = 'sqlite::memory:';
        $conn->connect(array('dsn' => $dsn));
        $engine = new Ganc_Engine($conn->getConnection($dsn));
        $engine->query(
            'CREATE TABLE users (' .
            'id integer primary key autoincrement,' .
            'name string not null,' .
            'password string not null' .
            ');'
        );
        $admin = array('id' => 1, 'name' => 'admin', 'password' => 'default');
        $user  = array('id' => 2, 'name' => 'user', 'password' => 'password');
        $engine->query("INSERT INTO users (id,name,password) VALUES (:id,:name,:password)", $admin);
        $engine->query("INSERT INTO users (id,name,password) VALUES (:id,:name,:password)", $user);
        $this->assertEquals(2, $engine->lastInsertId());

        $this->assertEquals($admin, $engine->find("SELECT * FROM users"));
        $this->assertEquals($user, $engine->find("SELECT * FROM users WHERE id=?", array(2)));

        $this->assertEquals(
            array($admin, $user),
            $engine->findAll("SELECT * FROM users")
        );
        $this->assertEquals(
            array($admin, $user),
            $engine->findAll("SELECT * FROM users WHERE id IN (?,?)", array(1, 2))
        );

        $engine->beginTransaction();
        $engine->query("DELETE FROM users");
        $this->assertEquals(
            array(),
            $engine->findAll("SELECT * FROM users")
        );
        $engine->rollBack();
        $this->assertEquals(
            array($admin, $user),
            $engine->findAll("SELECT * FROM users")
        );

        $engine->beginTransaction();
        $engine->query("DELETE FROM users");
        $engine->commit();
        $this->assertEquals(
            array(),
            $engine->findAll("SELECT * FROM users")
        );

        //echo (string)$engine->logger;
    }
Exemplo n.º 2
0
        foreach ($row as $key => $val) {
            $this->$key = $val;
        }
        return $this;
    }
}

class User extends Ganc_Object {
    public $table = 'users';
    public $columns = array('id', 'name', 'password');
}

$conn = new Ganc_ConnectionManager();
$dsn = 'sqlite::memory:';
$conn->connect(array('dsn' => $dsn));
$engine = new Ganc_Engine($conn->getConnection($dsn));
$engine->query(
    'CREATE TABLE users (' .
    'id integer primary key autoincrement,' .
    'name string not null,' .
    'password string not null' .
    ');'
);
$engine->insert('users', array('id' => 1, 'name' => 'admin', 'password' => 'default'));
$engine->insert('users', array('id' => 2, 'name' => 'user', 'password' => 'password'));

$user = new User();
$user->find(2);

echo 'id       : ' . $user->id . PHP_EOL;
echo 'name     : ' . $user->name . PHP_EOL;