Created
August 21, 2013 16:09
-
-
Save ceeram/6296515 to your computer and use it in GitHub Desktop.
rawSave()
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 | |
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