Max YO 5 rokov pred
rodič
commit
813070ec23

+ 7 - 0
src/Database/Sql/SqlTable.php

@@ -181,6 +181,13 @@ class SqlTable
 		if (! empty($options['CALC_FOUND_ROWS'])) {
 			$select_sql = 'SQL_CALC_FOUND_ROWS ' . $select_sql;
 		}
+		if (! empty($options['where'])) {
+			if (empty($where)) {
+				$where = $options['where'];
+			} else {
+				$where = array_merge($where, $options['where']);
+			}
+		}
 
 		$joins_sql = '';
 		if (! empty($options['join'])) {

+ 152 - 0
src/Database/Sql/SqlTableModel.php

@@ -0,0 +1,152 @@
+<?php
+
+namespace KarmaFW\Database\Sql;
+
+use \KarmaFW\App;
+
+
+class SqlTableModel
+{
+	public static $table_name = '';
+	public static $primary_key = [];
+
+
+	public static function getDb()
+	{
+		return App::getDb();
+	}
+
+
+	public static function checkTable()
+	{
+		if (empty(static::$table_name)) {
+			throw new \Exception("no table_name defined in " . get_called_class(), 1);
+		}
+	}
+
+
+	public static function all($where=[], $options=[])
+	{
+		// Alias of getAll
+		return static::getAll($where, $options);
+	}
+
+	public static function select($where=[], $options=[])
+	{
+		// Alias of getAll
+		return static::getAll($where, $options);
+	}
+
+	public static function selectAll($where=[], $options=[])
+	{
+		// Alias of getAll
+		return static::getAll($where, $options);
+	}
+
+	public static function get($where=[], $options=[])
+	{
+		// Alias of getAll
+		return static::getAll($where, $options);
+	}
+
+	public static function getAll($where=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+
+		return $db->getTable(static::$table_name)->select($where, $options);
+	}
+
+
+	public static function selectCount($where=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+
+		return $db->getTable(static::$table_name)->selectCount($where, $options);
+	}
+
+
+
+	public static function one($where=[], $options=[])
+	{
+		// Alias of getOne
+		return static::getOne($where, $options);
+	}
+
+	public static function selectOne($where=[], $options=[])
+	{
+		// Alias of getOne
+		return static::getOne($where, $options);
+	}
+
+	public static function getOne($where=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->selectOne($where, $options);
+	}
+
+
+	
+	public static function insert($values=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->insert($values, $options);
+	}
+	
+	public static function insertAll($rows=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->insertAll($rows, $options);
+	}
+
+	public static function update($updates=[], $where=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->update($updates, $where, $options);
+	}
+		
+	public static function delete($where=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->delete($where, $options);
+	}
+
+
+
+	// load a table row by his primary key. (usage: `$user = User::load($user_id);` )
+	public static function load($pk_where=[], $options=[])
+	{
+		if (empty($pk_where)) {
+			return null;
+		}
+
+		if (count(static::$primary_key) == 1 && is_scalar($pk_where)) {
+			$pk_where = [ static::$primary_key[0] => $pk_where ];
+		}
+
+		if (empty(static::$primary_key)) {
+			throw new \Exception("no primary_key defined in " . get_called_class(), 1);
+		}
+
+		$where = [];
+
+		foreach (static::$primary_key as $key) {
+			if (! isset($pk_where[$key])) {
+				throw new \Exception("missing value for primary_key " . $key . " in " . get_called_class(), 1);
+			}
+			$value = $pk_where[$key];
+
+			$where[$key] = $value;
+		}
+
+		return static::getOne($where, $options);
+
+	}
+
+}