Skip to content

Instantly share code, notes, and snippets.

@anzawi
Created September 13, 2018 20:53
Show Gist options
  • Save anzawi/b6194632700b56406ea8649a12b52d17 to your computer and use it in GitHub Desktop.
Save anzawi/b6194632700b56406ea8649a12b52d17 to your computer and use it in GitHub Desktop.
<?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