| Our goal
|
SQL
|
SQL packed in PHP
|
dibi
|
dibi fluent
|
NotORM
|
Doctrine
|
Nette Database
|
| DB connection
|
/* Move along, nothing to see here */
|
$link = mysql_connect($host, $username, $password); mysql_select_db($dbname, $link);
|
$options = array(
"driver" => $driver,
"host" => $host,
"username" => $username,
"password" => $password,
"database" => $dbname,
);
// Static registry variant
$dibi = dibi::connect($options);
// Standalone object variant
$dibi = new DibiConnection($options);
|
// same as non-fluent
// You do not have to use static dibi::methods(), but you can do:
$fluent = dibi::command();
// and use $fluent->methods() variations
// For example
dibi::select()
// is the same as
$fluent->select();
|
$pdo = new PDO("$driver:host=$host;dbname=$dbname", $username, $password); $notorm = new NotORM($pdo);
|
$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration( array(__DIR__ . "/Entities"), // directories with entitites TRUE, // development mode __DIR__ . '/Proxies' // proxies directory );
// Database connection information $connectionOptions = array( 'driver' => $driver, 'host' => $host, 'username' => $username, 'password' => $password, 'database' => $dbname, 'charset' => 'utf-8', );
// Create EntityManager $entityManager = Doctrine\ORM\EntityManager::create($connectionOptions, $config);
|
use Nette\Database\Connection;
$ndb = new Connection($dsn, $user, $password);
|
| COUNT(*)
|
SELECT COUNT(*) FROM `table`
|
$queryResult = mysql_query("SELECT COUNT(*) FROM `table`");
$count = mysql_result($queryResult, 0);
|
$count = dibi::query("SELECT COUNT(*) FROM table")->fetchSingle();
|
$count = dibi::select("COUNT(*)")->from("table")->fetchSingle();
// OR (but be careful, see http://forum.dibiphp.com/cs/792-velky-problem-s-dibidatasource-a-mysql)
dibi::select("*")->from("table")->count();
|
$count = $notorm->table()->count("*");
|
$count = $entityManager->createQuery('SELECT COUNT(e.id) FROM Entity e')->getSingleScalarResult();
|
$count = $ndb->table("table")->count("*");
|
| WHERE
|
SELECT * FROM `table` WHERE `column` = 'value'
|
$value = "value";
$safeValue = mysql_real_escape_string($value);
$queryResult = mysql_query("SELECT * FROM `table` WHERE `column` = '" . $safeValue . "'");
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[] = $row;
}
|
$value = "value";
$result = dibi::query("SELECT * FROM table WHERE column = %s", $value)->fetchAll();
|
$value = "value";
$result = dibi::select("*")->from("table")->where("column = %s", $value)->fetchAll();
|
$value = "value";
$result = $notorm->table("column", $value);
// OR alternative
$result = $notorm->table(array("column" => $value));
// OR for primary id
$result = $notorm->table($value);
// column != value
$result = $notorm->table("NOT column", $value);
// column > value
$result = $notorm->table("column > ?", $value);
// column LIKE %value
$result = $notorm->table("column LIKE ?", "%$value");
|
$result = $entityManager->getRepository('Entity')
->findBy(array('column' => 'value'));
|
$value = "value";
$result = $ndb->table("table")->where("column", $value);
// OR alternative
$result = $ndb->table("table")->where(array("column" => $value));
// column > value
$result = $ndb->table("table")->where("column > ?", $value);
// column != value
$result = $ndb->table("table")->where("column != ?", $value);
|
| WHERE (AND)
|
SELECT * FROM `table` WHERE `column` = 'value' AND `column2` = 'value2'
|
$value1 = "value";
$value2 = "value2";
$queryResult = mysql_query("SELECT * FROM `table` WHERE `column` = '" . mysql_real_escape_string($value1) . "' AND `column2` = '" . mysql_real_escape_string($value2) . "'");
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[] = $row;
}
|
$value = "value";
$value2 = "value2";
$result = dibi::query("SELECT * FROM table WHERE column = %s AND column2 = %s", $value, $value2)->fetchAll();
|
$value = "value";
$value2 = "value2";
$result = dibi::select("*")->from("table")->where("column = %s AND column2 = %s", $value, $value2)->fetchAll();
// OR
$result = dibi::select("*")->from("table")->where("column = %s", $value)->and("column2 = %s", $value2)->fetchAll();
// OR
$values = array(
"column" => "value",
"column2" => "value2"
);
$result = dibi::select("*")->from("table")->where($values)->fetchAll();
|
$value = "value";
$value2 = "value2";
$result = $notorm->table()->where("column = ? AND column2 = ?", $value, $value2);
// OR
$result = $notorm->table()->where(array("column" => $value,"column2" => $value2));
// OR
$result = $notorm->table()->where("column", $value)->where("column2", $value2);
|
$result = $entityManager->getRepository('Entity')
->findBy(array('column' => 'value', 'column2' => 'value2'));
// but when equals isn't enought, you have to use DQL
$result = $entityManager
->createQuery('SELECT e FROM Entity e WHERE e.column = :val1 AND e.column_2 != :val2')
->setParameters(array('val1' => 'value', 'val2' => 'value_2'))
->getResult();
|
$value = "value";
$value2 = "value2";
$result = $ndb->table("table")->where("column = ? AND column2 = ?", $value, $value2);
// OR
$result = $ndb->table("table")->where(array("column" => $value,"column2" => $value2));
// OR
$result = $ndb->table("table")->where("column", $value)->where("column2", $value2);
|
| WHERE (OR)
|
SELECT * FROM `table` WHERE `column` = 'value' OR `column2` = 'value2'
|
$value1 = "value"; $value2 = "value2";
$queryResult = mysql_query("SELECT * FROM `table` WHERE `column` = '" . mysql_real_escape_string($value1) . "' OR `column2` = '" . mysql_real_escape_string($value2) . "'"); $result = array(); while ($row = mysql_fetch_assoc($queryResult)) { $result[] = $row; }
|
$value = "value";
$value2 = "value2";
$result = dibi::query("SELECT * FROM table WHERE column = %s OR column2 = %s", $value, $value2)->fetchAll();
// OR
$result = dibi::query("SELECT * FROM table WHERE %or", array(
"column" => $value,
"column2" => $value2
))->fetchAll();
|
$value = "value";
$value2 = "value2";
$result = dibi::select("*")->from("table")->where("column = %s OR column2 = %s", $value, $value2)->fetchAll();
// OR
$result = dibi::select("*")->from("table")->where("column = %s", $value)->or("column2 = %s", $value2)->fetchAll();
// OR
$values = array(
"column" => "value",
"column2" => "value2"
);
$result = dibi::select("*")->from("table")->where("%or", $values)->fetchAll();
|
$value = "value";
$value2 = "value2";
$result = $notorm->table()->where("column = ? OR column2 = ?", $value, $value2);
|
$result = $entityManager ->createQuery('SELECT e FROM Entity e WHERE e.column = :val1 OR e.column2 = :val2') ->setParameters(array('val1' => 'value', 'val2' => 'value2')) ->getResult();
|
$value = "value";
$value2 = "value2";
$result = $ndb->table("table")->where("column = ? OR column2 = ?", $value, $value2);
|
| WHERE (IN)
|
SELECT * FROM `table` WHERE `column` IN ('value', 'value2')
|
$values = array('value', 'value2');
$safeValues = array_map('mysql_real_escape_string', $values); $queryResult = mysql_query("SELECT * FROM `table` WHERE `column` IN ('" . implode("', '", $safeValues) . "') "); $result = array(); while ($row = mysql_fetch_assoc($queryResult)) { $result[] = $row; }
|
$array = array("value", "value2");
$result = dibi::query("SELECT * FROM table WHERE column IN %in", $array)->fetchAll();
|
$array = array("value", "value2");
$result = dibi::select("*")->from("table")->where("column")->in($values)->fetchAll();
|
$array = array("value", "value2");
$result = $notorm->table("column", $array);
// for more conditions
$result = $notorm->table("column", $array)->where("column2", $array2);
// OR
$result = $notorm->table()->where("column = ? AND column_2 = ?",$array,$array_2);
// for even more conditions
$result = $notorm->table("column",$array)->where("column_2",$array_2)->where("column_3","value_3");
// NOT IN
$result = $notorm->table("NOT column",$array);
|
$result = $entityManager ->createQuery('SELECT e FROM Entity e WHERE e.column = :val1 OR e.column2 = :val2') ->setParameters(array('val1' => 'value', 'val2' => 'value2')) ->getResult();
|
$array = array("value", "value2");
$result = $ndb->table("table")->where("column", $array);
|
| WHERE (LIKE)
|
SELECT * FROM `table` WHERE `column` LIKE 'value' // OR SELECT * FROM `table` WHERE `column` LIKE '%value' // OR SELECT * FROM `table` WHERE `column` LIKE '%value%'
|
$value = "value";
$queryResult = mysql_query("SELECT * FROM `table` WHERE `column` LIKE '%" . mysql_real_escape_string($value1) . "'"); $result = array(); while ($row = mysql_fetch_assoc($queryResult)) { $result[] = $row; }
|
$value = "value";
$result = dibi::query("SELECT * FROM table WHERE column LIKE %~like", $value)->fetchAll();
|
$value = "value";
$result = dibi::select("*")->from("table")->where("column LIKE %~like", $value)->fetchAll();
|
$value = "value";
$result = $notorm->table()->where("column LIKE ?","%$value");
|
$result = $entityManager ->createQuery('SELECT e FROM Entity e WHERE e.column LIKE :val1') ->setParameter('val1' => '%value') ->getResult();
// You have to place percents in parametr value
|
$value = "value";
$result = $ndb->table("table")->where("column LIKE ?","%$value");
|
| LIMIT, OFFSET
|
SELECT * FROM `table` LIMIT 10 OFFSET 5
|
$limit = 10;
$offset = 5;
$queryResult = mysql_query("SELECT * FROM `table` LIMIT " . intval($limit) . " OFFSET " . intval($offset));
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[] = $row;
}
|
$limit = 10;
$offset = 5;
$result = dibi::query("SELECT * FROM table %lmt %ofs", $limit, $offset)->fetchAll();
|
$limit = 10;
$offset = 5;
$result = dibi::select("*")->from("table")->limit($limit)->offset($offset)->fetchAll();
|
$limit = 10;
$offset = 5;
$result = $notorm->table()->limit($limit [, $offset]);
|
$limit = 10;
$offset = 5;
$result = $entityManager
->createQuery('SELECT e FROM Entity e')
->setMaxResults($limit)->setFirstResult($offset)
->getResult();
|
$limit = 10;
$offset = 5;
$result = $ndb->table("table")->limit($limit [, $offset]);
|
| INSERT
|
INSERT INTO `table` (`column`) VALUES ('value')
|
$array = array( "column" => "value" );
$safeArray = array_map('mysql_real_escape_string', $array); mysql_query("INSERT INTO `table` (`" . implode("`, `", array_keys($safeArray)) . "`) VALUES ('" . implode("', '", $safeArray) . "')");
|
$array = array(
"column" => "value"
);
dibi::query("INSERT INTO table %v", $array);
|
$array = array(
"column" => "value"
);
dibi::insert("table", $array)->execute();
|
$array = array(
"column" => "value"
);
$notorm->table()->insert($array);
|
$entity = new Entity; $entity->column = "value"; $entityManager->persist($entity); $entityManager>flush();
// Learn about entities: http://www.doctrine-project.org/docs/orm/2.0/en/reference/architecture.html#entities
|
$array = array(
"column" => "value"
);
$ndb->table("table")->insert($array);
|
| Last record id
|
SELECT LAST_INSERT_ID()
|
$id = mysql_insert_id();
|
$id = dibi::getInsertId();
|
// same as non-fluent
$id = dibi::getInsertId();
|
$row = $notorm->table()->insert(...);
$id = $row["id"];
|
// Doctrine does assign ids to entities automaticaly
|
$row = $ndb->table("table")->insert(...);
$id = $row["id"];
|
| UPDATE
|
UPDATE `table` SET `column` = 'value' WHERE `column2` = 'value2'
|
$array = array(
"column" => "value"
);
$value = "value_2";
$set = array();
foreach ($array as $col => $val) {
if ($val === NULL) {
$set[] = "`" . $col . "` = NULL";
} else {
$set[] = "`" . $col . "` = '" . mysql_real_escape_string($val) . "'";
}
}
mysql_query("UPDATE `table` SET " . implode(", ", $set) . " WHERE column_2 = ''" . mysql_real_escape_string($value) . "");
|
$id = 123;
$array = array(
"column" => "value"
);
dibi::query("UPDATE table SET %a", $array, "WHERE column2 = %i", $id);
|
$id = 123;
$array = array(
"column" => "value"
);
dibi::update("table", $array)->where("column2 = %i", $id)->execute();
|
$id = 123;
$array = array(
"column" => "value"
);
$notorm->table("column2", $id)->update($array);
// OR for primary key
$notorm->table($id)->update($array);
|
$entityManager
->createQuery("UPDATE Entity e SET e.column = :val WHERE u.column_2 = :val2")
->setParameters(array('val' => "value", 'val2' = "value_2"))
->execute();
// OR
$entity = $entityManager->getRepository('Entity')->find(1);
$entity->column = "value";
$entityManager->flush();
// OR
$entityManager->transactional(function ($entityManager) {
$entity = $entityManager->getRepository('Entity')->find(1);
$entity->column = "value";
});
// Learn locking: http://www.doctrine-project.org/docs/orm/2.0/en/reference/transactions-and-concurrency.html#locking-support
|
$id = 123;
$array = array(
"column" => "value"
);
$ndb->table("table")->where("column2", $id)->fetch()->update($array);
// OR for primary key
$ndb->table("table")->get($id)->update($array);
|
| DELETE
|
DELETE FROM `table` WHERE `column` = 'value'
|
$value = "value";
mysql_query("DELETE FROM `table` WHERE `column` = '" . mysql_real_escape_string($value) . "'");
|
$id = 123;
dibi::query("DELETE FROM table WHERE column = %i", $id);
|
$id = 123;
dibi::delete("table")->where("column = %i", $id)->execute();
|
$id = 123;
$notorm->table("column", $id)->delete();
// OR for primary key
$notorm->table($id)->delete();
|
$entityManager ->createQuery("DELETE Entity e WHERE u.column = :val") ->setParameter('val', "value") ->execute();
// OR $entity = $entityManager->getRepository('Entity') ->findOneBy(array('column' => "value")); $entityManager->remove($entity); $entityManager->flush();
|
$id = 123;
$ndb->table("table")->where("column", $id)->fetch()->delete();
|
| INSERT ON DUPLICATE KEY UPDATE (UPSERT)
|
INSERT INTO `table` (`column`) VALUES (`value`) ON DUPLICATE KEY UPDATE `column` = VALUES(`column`)
|
$array = array(
"column" => "value"
);
$safeArray = array_map("mysql_real_escape_string", $array);
$value = "value2";
mysql_query("INSERT INTO `table` (`" . implode("`, `", array_keys($safeArray)) . "`) VALUES ('" . implode("', '", $safeArray) . "') ON DUPLICATE KEY UPDATE `column` = `" . mysql_real_escape_string($value) . "`");
|
$id = 123;
$array = array(
"column" => "value"
);
dibi::query("INSERT INTO table %v", $array, "ON DUPLICATE KEY UPDATE column = %i", $id);
|
$id = 123;
$array = array(
"column" => "value"
);
dibi::insert("table", $array)->onDuplicateKeyUpdate("column = %i", $id)->execute();
|
$id = 123;
// unique key
$unique = array(
"column" => $id
);
// insert values if the row doesn't exist
$insert = array(
"column2" => "value2"
);
// update values otherwise
$update = array(
"column3" => "value3"
);
$notorm->table()->insert_update($unique,$insert,$update);
|
// Learn merge: http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-objects.html#merging-entities
|
// not implemented
|
| Single value
|
SELECT `column` FROM `table` LIMIT 1
|
$result = mysql_query("SELECT `column` FROM `table` LIMIT 1");
$column = mysql_result($result, 0);
|
$column = dibi::query("SELECT column FROM table")->fetchSingle();
// limit clause is added automatically when fetchSingle() is used
|
$column = dibi::select("column")->from("table")->fetchSingle();
// limit clause is added automatically when fetchSingle() is used
|
$result = $notorm->table(...)->select("column")->fetch();
$column = $result["column"];
// OR edit NotORM/Result.php
// https://github.com/vrana/notorm/pull/33/files
// so you can do this
$column = $notorm->table(...)->fetchSingle("column");
|
$column = $entityManager
->createQuery('SELECT PARTIAL e.{column} FROM Entity e')
->setMaxResults(1)
->getSingleScalarResult();
|
$result = $ndb->table("table")->select("column")->fetch();
$column = $result["column"];
|
| Values in associative array
|
SELECT `id`, `value` FROM `table`
|
$queryResult = mysql_query("SELECT `id`, `value` FROM `table`"); $result = array(); while ($row = mysql_fetch_assoc($queryResult)) { $result[$row['id']] = $row['value']; }
|
$array = dibi::query("SELECT id, column FROM table")->fetchPairs();
// OR explicitly specify column names
$array = dibi::query("SELECT id, column, column2 FROM table")->fetchPairs("id", "column");
// OR automaticaly indexed array
$array = dibi::query("SELECT column FROM table")->fetchPairs(NULL, "column");
|
$array = dibi::select("id, column")->from("table")->fetchPairs();
// OR explicitly specify column names
$array = dibi::select("id, column")->from("table")->fetchPairs("id", "column");
// OR automaticaly indexed array
$array = dibi::select("column")->from("table")->fetchPairs(NULL, "column");
|
$array = $notorm->table()->fetchPairs("id","value");
|
$entities = $entityManager ->createQuery('SELECT PARTIAL e.{id, column} FROM Entity e') ->getResult(); $result = array(); foreach ($entities as $entity) { $result[$entity->id] = $entity->column; }
|
$array = $ndb->table("table")->fetchPairs("id","value");
|
| Rows in associative array
|
SELECT * FROM `table`
|
$queryResult = mysql_query("SELECT * FROM `table`");
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[$row['id']] = $row;
}
|
$result = dibi::query("SELECT * FROM table")->fetchAssoc("id");
/* dibi's fetchAssoc has such an amazing flexibility, that has no equivalent in other DBAL's/ORM's,
because it can structurate data in deeply asociative arrays */
|
$column = dibi::select("*")->from("table")->fetchAssoc("id");
|
$rowsArray = $notorm->table()->fetchPairs("id");
|
$entities = $entityManager
->createQuery('SELECT e FROM Entity e')
->getResult();
// OR
$entities = $entityManager->getRepository('Entity')->findAll();
$result = array();
foreach ($queryResult as $entity) {
$result[$entity->id] = $entity;
}
|
$rowsArray = $ndb->table("table")->fetchPairs("id");
|
| JOIN
|
SELECT * FROM `table` JOIN `table2` ON `table`.`id` = `table2`.`table`
|
$queryResult = mysql_query("SELECT * FROM `table` JOIN `table2` ON `table`.`id` = `table2`.`table`");
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[] = $row;
}
|
$result = dibi::query("SELECT * FROM table JOIN table2 ON table.id = table2.table")->fetchAll();
|
$result = dibi::select("*")->from("table")->join("table2")->on("table.id = table2.table")->fetchAll();
// left join
$result = dibi::select("*")->from("table")->leftJoin("table2")->on("table.id = table2.table")->fetchAll();
|
// table.table2_id joins on table2.id
$result = $notorm->table()->select("table.*, table2.column");
/**
* Multiple join:
* table.table2_id joins on table2.id
* table2.table3_id joins on table3.id
*/
$result = $notorm->table()->select("table.*, table2.column, table2.table3.column2");
// OR (for non-primary keys) edit NotORM/Result.php
// https://github.com/vrana/notorm/pull/32/files
// so you can do this
$notorm->table()->join("table2", "LEFT JOIN table2 ON table.user_id = table2.car_id")
->select("table.*, table2.*");
// you have to specify what to select, otherwise table.* is used as default
|
// Doctrine does fetch relations lazily, when you access them. But you can join them manualy
$result = $entityManager->createQuery('SELECT e, r FROM Entity e JOIN e.related r')->getResult();
// Learn associations: http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html
|
$result = $ndb->table("table")->where("table2.column = ?", $value);
|
| UNION
|
SELECT * FROM `table` UNION SELECT * FROM `table2`
|
$queryResult = mysql_query("SELECT * FROM `table` UNION SELECT * FROM `table2`");
$result = array();
while ($row = mysql_fetch_assoc($queryResult)) {
$result[] = $row;
}
|
$result = dibi::query("SELECT * FROM table UNION SELECT * FROM table2")->fetchAll();
|
// to reach expected query is a little bit tricky
$fluent1 = dibi::select("*")->from("table1");
$fluent2 = dibi::select("*")->from("table2");
$result = $fluent1->union($fluent2);
// produce SELECT * FROM "table1" UNION ( SELECT * FROM "table2" )
$result = dibi::command()->{''}($fluent1)->union($fluent2);
// produce ( SELECT * FROM "table1" ) UNION ( SELECT * FROM "table1" )
|
$result = $notorm->table()->select(...)->where(...)->union(
$notorm->table2()->select(...)->where(...)
);
|
// Learn inheritance: http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html
|
// not available (yet)
|