Skip to content

Instantly share code, notes, and snippets.

@ceeram
Created August 21, 2013 16:09
Show Gist options
  • Save ceeram/6296515 to your computer and use it in GitHub Desktop.
Save ceeram/6296515 to your computer and use it in GitHub Desktop.
rawSave()
<?php
App::uses('Model', 'Model');
class AppModel extends Model {
public $saveTemplate = 'INSERT INTO `%s` (%s) VALUES %s;';
public function rawSave($data) {
if(empty($data)) {
return true;
}
$data = Set::extract('{n}.' . $this->alias, $data);
$schema = $this->schema();
unset($schema['id']);
$keyData = '`' . implode('`, `', array_keys($schema)) . '`';
$db = $this->getDataSource();
foreach($data as $k => $row) {
foreach ($row as $field => $value) {
$row[$field] = $db->value($value, $field);
}
$data[$k] = "(" . implode(", ", $row) . ")";
}
$data = sprintf($this->saveTemplate, $this->table, $keyData, implode(', ', $data));
$this->query($data);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment