Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Batch Mockup
*
* I placed this in my BaseController.php file
*
* @usage
* $batch = new Batch('stats');
* $batch->columns = ['score', 'name'];
* $batch->data = [
* [1, 'john'],
* [4, 'fred'],
* [1, 'mickey'],
* ];
* $batch->insert();
*
*/
class BatchInsert
{
public $table = null;
public $columns = array();
public $data = array();
public $insert_string = '';
// --------------------------------------------------------------
public function __construct($table = false)
{
if ($table) {
$this->table = $table;
}
$di = Phalcon\DI::getDefault();
$this->db = $di->get('db');
}
// --------------------------------------------------------------
public function insert()
{
$this->_validate();
$this->_stringify();
//echo $this->insert_string;
try {
$result = $this->db->execute($this->insert_string);
} catch (Exception $e) {
echo $e->getMessage();
}
$this->insert_string = '';
return $result;
}
// --------------------------------------------------------------
private function _stringify()
{
$columns = sprintf('`%s`', implode('`,`', $this->columns));
$str = '';
foreach ($this->data as $values) {
foreach ($values as &$val) {
if (is_null($val)) {
$val = 'NULL';
continue;
}
if (is_string($val)) {
$val = "'".$val."'";
}
}
$str .= sprintf('(%s),', implode(',', $values));
}
$str = rtrim($str, ',');
$query = sprintf("INSERT INTO `%s` (%s) VALUES %s",
$this->table,
$columns,
$str
);
$this->insert_string = $query;
}
private function _makeValues($item)
{
print_r($item);
}
// --------------------------------------------------------------
private function _validate()
{
if ($this->table == null) {
throw new \Exception('Batch Table must be defined');
}
if (count($this->columns) == 0) {
throw new \Exception('Batch Columns cannot be empty');
}
$required_count = count($this->columns);
foreach ($this->data as $value) {
if (count($value) !== $required_count) {
throw new \Exception('Batch Data must match the same column count of ' . $required_count);
}
}
}
// --------------------------------------------------------------
}
@afischoff

This comment has been minimized.

Copy link
Owner Author

afischoff commented Mar 17, 2014

This adds support for other data types (INT and NULL) in the _stringify() method.

@JREAM

This comment has been minimized.

Copy link

JREAM commented Mar 17, 2014

Awesome nice!

@jimmycdinata

This comment has been minimized.

Copy link

jimmycdinata commented Dec 17, 2014

Is this safe from SQL injections attempts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.