<?php
 namespace App\Utils\Db\ManageRecords; class Update extends InsertUpdateAbstract { private $populatingTableAlias; public function __construct($populatingTableName, $populatingTableAlias, ?array $activeColumns = null) { if (!$populatingTableName) { throw new \Exception('You must set the populating table name.'); } if (!$populatingTableAlias) { throw new \Exception('You must set the populating table alias.'); } $this->populatingTableName = $populatingTableName; $this->populatingTableAlias = $populatingTableAlias; if ($activeColumns!==null) { $this->setActiveColumns($activeColumns); } } protected function getSQL() :array { $bindVars = []; $sql = "UPDATE ".$this->getSQL_tables(); $sql .= ' SET '.$this->getSQL_set(); $whereSQL = $this->getSQL_where(); if ($whereSQL['sql']) { $sql .= " WHERE ".$whereSQL['sql']; $bindVars = $whereSQL['bindVars']; } return [ 'sql'=>$sql, 'bindVars'=>$bindVars ]; } protected function getSQL_tables() { $return = "`{$this->populatingTableName}` AS {$this->populatingTableAlias}"; foreach($this->sqlData['tables'] as $tbl=>$nada) { if ($tbl!=$this->populatingTableAlias) { if ($tbl and !$this->joinTablesList[$tbl]) { throw new \Exception("Invalid table alias joined: `{$tbl}`"); } $tblDet = $this->joinTablesList[$tbl]; $return .= " {$tblDet['joinType']} JOIN `{$tblDet['table']}` AS {$tbl} ON ({$tblDet['foreignKeyRef']})"; } } return $return; } private function getSQL_set() { $columnsList = []; foreach ($this->sqlData['columns'] as $fieldColum=>$dataColumn) { $columnsList[] = "{$fieldColum} = {$dataColumn}"; } return implode(', ', $columnsList); } }