2015-05-06 19:25:57 +00:00
|
|
|
<?php
|
2015-08-29 15:43:13 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* FuzeWorks.
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
|
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 TechFuze
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2016-05-07 17:22:09 +00:00
|
|
|
* @author TechFuze
|
|
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
|
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
|
|
*
|
|
|
|
* @link http://fuzeworks.techfuze.net
|
|
|
|
* @since Version 0.0.1
|
|
|
|
*
|
|
|
|
* @version Version 0.0.1
|
2015-08-29 15:43:13 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-06 19:25:57 +00:00
|
|
|
namespace Module\DatabaseUtils;
|
2016-05-07 17:22:09 +00:00
|
|
|
|
|
|
|
use FuzeWorks\Module;
|
|
|
|
use FuzeWorks\Modules;
|
|
|
|
use FuzeWorks\ModelServer;
|
|
|
|
use FuzeWorks\DatabaseException;
|
|
|
|
use FuzeWorks\Config;
|
|
|
|
use FuzeWorks\Logger;
|
|
|
|
use PDOException;
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2015-08-29 15:43:13 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Main class of the database utilities and model providers.
|
|
|
|
*
|
|
|
|
* @author Abel Hoogeveen <abel@techfuze.net>
|
|
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
2015-08-29 15:43:13 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
class Main implements ModelServer
|
|
|
|
{
|
|
|
|
use Module;
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public $fields = array();
|
|
|
|
public $primary = 'id';
|
|
|
|
public $table = '';
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function onLoad()
|
|
|
|
{
|
|
|
|
include_once self::getModulePath().'/class.query.php';
|
2015-05-06 19:25:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
public function giveModel($type)
|
|
|
|
{
|
|
|
|
return new Model();
|
2015-05-06 19:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Class Model.
|
2015-05-06 19:25:57 +00:00
|
|
|
*
|
|
|
|
* Models provide an easy connection to database tables. Each table requires its own model. You don't need to worry about your queries or syntax anymore, because
|
|
|
|
* models will handle queries and error handling. Because of this, you can freely change your database infrastructure without fear of needing to change your
|
|
|
|
* table names at thousands of places. The model is the only place where you have to change your database names or fields.
|
|
|
|
*
|
|
|
|
* Models also allow custom methods to be created on them. You can use those methods to create specific operations or joins and then use the newly created method
|
|
|
|
* everywhere in your project. The code is at one place, the usages all over your project. Isn't that great?
|
|
|
|
*
|
2016-05-07 17:22:09 +00:00
|
|
|
* @author GOScripting
|
|
|
|
* @copyright Copyright (c) 2014 - 2015, GOScripting B.V. (http://goscripting.com)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
|
|
*
|
|
|
|
* @link http://goframework.net
|
2015-05-06 19:25:57 +00:00
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
class Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The name of the database table
|
|
|
|
*/
|
|
|
|
public $table = '';
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Traditional query interface.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param null $binds
|
|
|
|
*
|
|
|
|
* @return mixed returns fetched rows if available, otherwise returns number of affected rows
|
|
|
|
*
|
|
|
|
* @throws DatabaseException
|
|
|
|
*/
|
|
|
|
public function query($query, $binds = null)
|
|
|
|
{
|
|
|
|
if (Config::get('database')->debug) {
|
|
|
|
Logger::log('Manuel Query: '.$query, 'Database Model');
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
try {
|
|
|
|
$sth = Modules::get('core/database')->prepare($query);
|
|
|
|
if ($binds === null) {
|
|
|
|
$sth->execute();
|
|
|
|
} else {
|
|
|
|
$sth->execute($binds);
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
throw new DatabaseException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
if ($sth->columnCount() > 0) {
|
2015-05-06 19:25:57 +00:00
|
|
|
// Fetch results
|
|
|
|
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
2016-05-07 17:22:09 +00:00
|
|
|
} else {
|
2015-05-06 19:25:57 +00:00
|
|
|
// Fetch number of affected rows
|
|
|
|
$result = $sth->rowCount();
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Set the table you wish to approach.
|
|
|
|
*
|
|
|
|
* @param string $table Table name
|
|
|
|
*/
|
|
|
|
public function setTable($table)
|
|
|
|
{
|
|
|
|
$this->table = $table;
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table.
|
|
|
|
*
|
|
|
|
* @see Query::select
|
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
public function select()
|
|
|
|
{
|
|
|
|
$queryBuilder = new Query();
|
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
call_user_func_array(array($queryBuilder, 'select'), func_get_args());
|
|
|
|
$queryBuilder->from();
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $queryBuilder;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table.
|
|
|
|
*
|
|
|
|
* @see Query::update
|
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$queryBuilder = new Query();
|
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
$queryBuilder->update($this->table);
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $queryBuilder;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table.
|
|
|
|
*
|
|
|
|
* @see Query::delete
|
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$queryBuilder = new Query();
|
|
|
|
$queryBuilder->setTable($this->table);
|
2015-05-06 19:25:57 +00:00
|
|
|
call_user_func_array(array($queryBuilder, 'delete'), func_get_args());
|
2016-01-23 13:08:29 +00:00
|
|
|
$queryBuilder->from();
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $queryBuilder;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* The default table will be set to $this->table.
|
|
|
|
*
|
|
|
|
* @see Query::insert
|
|
|
|
*
|
|
|
|
* @param $array Array with values
|
|
|
|
*
|
|
|
|
* @return Query
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function insert($array)
|
|
|
|
{
|
|
|
|
$queryBuilder = new Query();
|
|
|
|
$queryBuilder->setTable($this->table);
|
2015-05-06 19:25:57 +00:00
|
|
|
call_user_func_array(array($queryBuilder, 'insert'), func_get_args());
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
return $queryBuilder;
|
|
|
|
}
|
2015-05-06 19:25:57 +00:00
|
|
|
|
2015-10-11 18:14:49 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* The default table will be set to $this->table.
|
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @see Query::replace
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @param $array Array with values
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @return Query
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2015-10-11 18:14:49 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2016-05-07 17:22:09 +00:00
|
|
|
public function replace($array)
|
|
|
|
{
|
2015-10-11 18:14:49 +00:00
|
|
|
$queryBuilder = new Query();
|
|
|
|
$queryBuilder->setTable($this->table);
|
|
|
|
call_user_func_array(array($queryBuilder, 'replace'), func_get_args());
|
|
|
|
|
|
|
|
return $queryBuilder;
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Return latest insert id.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getLastInsertId()
|
|
|
|
{
|
|
|
|
return Modules::get('core/database')->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call($name, $params)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array(Modules::get('core/database'), $name), $params);
|
|
|
|
}
|
|
|
|
}
|