Hi
I'm developing a hands-off website CMS in CakePHP, and I want it to
email me a dump of the database every 6 months as a backup. I have
bodged the model_php5.php and dbo_source.php so they return the insert
statement of a model as a string, based on the save and create
functions but not running the sql. However I can't concatenate the
output.
The code:
function backup() {
$sql_string = array();
$sql_string2 = '';
$sql_string3 = '';
$i = 1;
$articles = $this->Article->findAll();
foreach ($articles as $article) {
$sql_string[$i] = $this->Article->saveString($article);
$sql_string2 .= $this->Article->saveString($article);
$i = $i + 1;
}
$sql_string3 = implode(";",$sql_string);
$this->Session->setFlash('$sql_string'.$sql_string[1].'-----'.
$sql_string2.'-----'.$sql_string3);
$this->redirect('/articles/index');
}
The array is fully populated, however as soon as I try to concatenate
the strings in the array, it becomes undisplayable - no error is
displayed but no string is displayed either. (In the case above none
of the three strings are displayed, but if I remove $sql_string2 and
3, the array entry is displayed. I've also tried $sql_string2 =
$sql_string2 . $this->Article->saveString($article);
Why won't the concatenate work? I'm completely baffled...
All time and help hugely appreciated thank you.
Chris
The bodge code is - in dbo_source.php - same as 'create' but returning
sql not executing it:
function createString(&$model, $fields = null, $values = null) {
$fieldInsert = array();
$valueInsert = array();
$id = null;
if ($fields == null) {
unset($fields, $values);
$fields = array_keys($model->data);
$values = array_values($model->data);
}
$count = count($fields);
for ($i = 0; $i < $count; $i++) {
$fieldInsert[] = $this->name($fields[$i]);
if ($fields[$i] == $model->primaryKey) {
$id = $values[$i];
}
}
$count = count($values);
for ($i = 0; $i < $count; $i++) {
$set = $this->value($values[$i], $model-
>getColumnType($fields[$i]));
if ($set === "''") {
unset ($fieldInsert[$i]);
} else {
$valueInsert[] = $set;
}
}
return 'INSERT INTO ' . $this->fullTableName($model) . ' (' .
join(',', $fieldInsert). ') VALUES (' . join(',', $valueInsert) . ')';
}
And in model_php5.php - the same as 'save' but calling my save db
function:
function saveString($data = null, $validate = true, $fieldList =
array()) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
if ($data) {
if (countdim($data) == 1) {
$this->set(array($this->name => $data));
} else {
$this->set($data);
}
}
$whitelist = !(empty($fieldList) || count($fieldList) == 0);
if ($validate && !$this->validates()) {
return false;
}
if (!$this->beforeSave()) {
return false;
}
$fields = $values = array();
if (count($this->data) > 1) {
$weHaveMulti = true;
$joined = false;
} else {
$weHaveMulti = false;
}
$habtm = count($this->hasAndBelongsToMany);
foreach ($this->data as $n => $v) {
if (isset($weHaveMulti) && isset($v[$n]) && $habtm > 0) {
$joined[] = $v;
} else {
if ($n === $this->name) {
foreach (array('created', 'updated',
'modified') as $field) {
if (array_key_exists($field, $v) &&
(empty($v[$field]) ||
$v[$field] === null)) {
unset($v[$field]);
}
}
foreach ($v as $x => $y) {
if ($this->hasField($x) && ($whitelist
&& in_array($x,
$fieldList) || !$whitelist)) {
$fields[] = $x;
$values[] = $y;
}
}
}
}
}
//set to false so always creates INSERT stmnt - could insert switch
here to create UPDATE strings (and use 'update' below)
$this->id = false;
return $db->createString($this, $fields, $values);
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---