Created
September 13, 2018 20:53
-
-
Save anzawi/b6194632700b56406ea8649a12b52d17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PHPtricks\Orm\DML; | |
trait Delete | |
{ | |
/** | |
* delete from table | |
* @return bool | |
*/ | |
public function delete() | |
{ | |
$results = (array)$this->_results; | |
// check if its empty | |
if(!count($results)) | |
{ | |
// try to call select() method | |
try | |
{ | |
$results = (array)$this->select()->results(); | |
if(count($results) == 1) $results = $results[0]; | |
} | |
catch (\Exception $e) | |
{ | |
return false; | |
} | |
} | |
if($this->count() == 1) | |
{ | |
return $this->remove($results); | |
} | |
for($i = 0; $this->count() > $i; $i++) | |
{ | |
$this->remove( $results[$i]); | |
} | |
return true; | |
} | |
private function remove($data) | |
{ | |
$this->_where = "WHERE"; | |
$x = 1; | |
foreach($data as $i => $row) | |
{ | |
if(!is_numeric($row)) | |
$this->_where .= " {$i} = '{$row}'"; | |
else | |
$this->_where .= " {$i} = {$row}"; | |
// add comma between values | |
if($x < count((array)$data)) { | |
$this->_where .= " AND"; | |
} | |
$x++; | |
} | |
$sql = "DELETE FROM $this->_table " . $this->_where; | |
return $this->query($sql); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment