File: phptricksORM/DML/Delete.php

Recommend this page to a friend!
  Classes of mohammad anzawi  >  PHP PDO database class  >  phptricksORM/DML/Delete.php  >  Download  
File: phptricksORM/DML/Delete.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP PDO database class
Access databases using PDO
Author: By
Last change: New Version (5.0.0)
Date: 7 months ago
Size: 1,839 bytes
 

Contents

Class file image Download
<?php
/**
 * *
 * * please don't remove this comment block
 * *
 * * @author phptricks Team - Mohammad Anzawi
 * * @author_uri https://phptricks.org
 * * @uri https://github.com/anzawi/php-database-class
 * * @version 5.0.0
 * * @licence MIT -> https://opensource.org/licenses/MIT
 * * @package PHPtricks\Orm
 *
 */

namespace PHPtricks\Orm\DML;

/**
 * Trait Delete
 *
 * @package 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;
    }

   
/**
     * @param $data
     *
     * @return mixed
     */
   
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);
    }

}


For more information send a message to info at phpclasses dot org.