I'm uncertain about proper OOP design since my teachers are explaining OOP very vaguely. Do I need to use the constructor and the mutators like this or am I better off removing the constructor and passing the information when the methods are called?
This is currently my dynamic sql class(extends the db connection)
class dbMainExec
{
private $_db;
private $_table;
private $_column;
private $_where;
private $_join;
private $_errors = array();
private $_stmt;
private $_results;
private $_countResults;
public function __construct($table)
{
$this->_db = new dbConn();
$this->setTable($table);
}
public function setTable($table)
{
$table = strtolower($table);
$this->_table = $table;
}
public function getTable()
{
return $this->_table;
}
public function setColumn($column)
{
if (is_array($column)) {
$this->_column = $column;
} else {
$this->_errors[] = "Columen is geen array!";
}
}
public function getColumn()
{
return $this->_column;
}
public function setWhere($where)
{
if (is_array($where) || empty($where)) {
$this->_where = $where;
} else {
$this->_errors[] = "where is geen array!";
}
}
public function getWhere()
{
return $this->_where;
}
public function setJoin($join)
{
if (is_array($join) || empty($join)) {
$this->_join = $join;
} else {
$this->_errors[] = "join is geen array!";
}
}
public function getJoin()
{
return $this->_join;
}
public function setResults($results)
{
if (is_array($results)) {
$this->_results = $results;
} else {
$this->_errors[] = "Fout bij resultaten";
}
}
public function getResults()
{
return $this->_results;
}
public function setCountResults($countResults)
{
if (is_numeric($countResults)) {
$this->_countResults = $countResults;
} else {
$this->_errors[] = "fout bij tellen resultaten";
}
}
public function getCountResults()
{
return $this->_countResults;
}
public function setStatement($stmt)
{
$this->_stmt = $stmt;
}
public function getStatement()
{
return $this->_stmt;
}
public function execute($sql,$select = false)
{
$this->setStatement($this->_db->connect()->prepare($sql));
if (!empty($this->getColumn()) && $select == FALSE ) {
foreach ($this->getColumn() as $singleColumn => $columnValue) {
$this->getStatement()->bindValue(':' . $singleColumn, $columnValue);
}
}
if (!empty($this->getWhere())) {
foreach ($this->getWhere() as $singleWhere => $whereValue) {
$this->getStatement()->bindValue(':' . $singleWhere, $whereValue);
}
}
if (!empty($this->getJoin())) {
foreach ($this->getJoin() as $singleJoin => $joinValue) {
$this->getStatement()->bindValue(':' . $singleJoin, $joinValue);
}
}
$this->getStatement()->execute();
$this->setColumn(array());
$this->setWhere(array());
$this->setJoin(array());
}
public function selectRow($column = null, $where = null, $join = null)
{
$this->setColumn($column);
$this->setWhere($where);
$this->setJoin($join);
if ($this->_errors) {
var_dump($this->_errors);
die();
}
$sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
if (!empty($this->getJoin())) {
foreach ($this->getJoin() as $extraTable => $extraValues) {
$sql .= "JOIN $extraTable";
foreach ($extraValues as $singleValue => $singleTarget) {
$sql .= " ON {$singleValue} = {$singleTarget} ";
}
}
}
if (!empty($this->getWhere())) {
$sql .= "WHERE ";
$arrayKeys = array_keys($this->getWhere());
foreach ($this->getWhere() as $option => $value) {
if ($option != end($arrayKeys)) {
$sql .= $option . " = :{$option} AND ";
} else {
$sql .= $option . " = :{$option}";
}
}
}
$this->execute($sql,$select = TRUE);
$this->setResults($this->getStatement()->fetch(PDO::FETCH_ASSOC));
$this->setCountResults($this->getStatement()->rowCount());
}
public function selectMultiple($column = null, $where = null, $join = null)
{
$this->setColumn($column);
$this->setWhere($where);
$this->setJoin($join);
if ($this->_errors) {
var_dump($this->_errors);
die();
}
$sql = "SELECT " . implode($this->getColumn(), ',') . " FROM " . $this->getTable() . " ";
if (!empty($this->getJoin())) {
foreach ($this->getJoin() as $extraTable => $extraValues) {
$sql .= "JOIN $extraTable";
foreach ($extraValues as $singleValue => $singleTarget) {
$sql .= " ON {$singleValue} = {$singleTarget} ";
}
}
}
if (!empty($this->getWhere())) {
$sql .= "WHERE ";
$arrayKeys = array_keys($this->getWhere());
foreach ($this->getWhere() as $option => $value) {
if ($option != end($arrayKeys)) {
$sql .= $option . " = :{$option} AND ";
} else {
$sql .= $option . " = :{$option}";
}
}
}
$this->execute($sql, $select = TRUE);
$this->setResults($this->getStatement()->fetchAll(PDO::FETCH_ASSOC));
$this->setCountResults($this->getStatement()->rowCount());
}
public function insert($column = null)
{
$this->setColumn($column);
if ($this->_errors) {
var_dump($this->_errors);
die();
}
$sql = "INSERT INTO {$this->getTable()} ( " . implode(', ', array_keys($this->getColumn())) . ") VALUES (";
$arrayKeys = array_keys($this->getColumn());
foreach ($this->getColumn() as $singleColum => $singleValue) {
$sql .= ":" . $singleColum;
if ($singleColum != end($arrayKeys)) {
$sql .= ", ";
}
}
$sql .= ')';
$this->execute($sql);
}
public function update($column = null, $where = null)
{
$this->setColumn($column);
$this->setWhere($where);
if ($this->_errors) {
var_dump($this->_errors);
die();
}
$sql = "UPDATE {$this->getTable()} SET ";
$arrayKeys = array_keys($this->getColumn());
foreach ($this->getColumn() as $singlecolum => $singleValue) {
$sql .= $singlecolum . " = :" . $singlecolum;
if ($singlecolum != end($arrayKeys)) {
$sql .= ", ";
}
}
if (!empty($this->getWhere())) {
$sql .= " WHERE ";
$arrayKeys = array_keys($this->getWhere());
foreach ($this->getWhere() as $option => $value) {
if ($option != end($arrayKeys)) {
$sql .= $option . " = :{$option} AND ";
} else {
$sql .= $option . " = :{$option}";
}
}
}
$this->execute($sql);
}
public function delete($where = null)
{
$this->setWhere($where);
if ($this->_errors) {
var_dump($this->_errors);
die();
}
$sql = "DELETE FROM {$this->getTable()}";
if (!empty($this->getWhere())) {
$sql .= " WHERE ";
$arrayKeys = array_keys($this->getWhere());
foreach ($this->getWhere() as $option => $value) {
if ($option != end($arrayKeys)) {
$sql .= $option . " = :{$option} AND ";
} else {
$sql .= $option . " = :{$option}";
}
}
}
$this->execute($sql);
}
}