Hi there, I have been very successfully following Bob's guide to creating Custom DB Tables at
http://bobsguides.com/custom-db-tables.html, however, I seem to have hit a stumbling block when working with the edit snippet.
I created the snippet EditQuote as code below and called
[[EditQuote? "eId=`3`]]
Output was
Could not find Quote with ID: 3
This is the EditQuote snippet in its original form which gave the results above:
<?php
/* Get the existing Quote */
$quoteId = $scriptProperties['quoteId'];
$quote = $modx->getObject('Quotation', array('id'=>$quoteId));
/* Show error message if quote is not found */
if (empty($quote)) {
return ('Could not find Quote with ID: ' . $quoteId);
}
if (isset($_POST['submit']) && $_POST['submit'] == 'MySubmitVar') {
/* Form has been submitted */
$quote->set('topic', $_POST['topic']);
$quote->set('quote', $_POST['quote']);
$quote->set('author', $_POST['author']);
if ( $quote->save()) {
$output = "Quote Created";
} else {
$output = "Error";
}
} else {
/* Not a repost, just display the form */
/* The second argument to getChunk() tells MODx to replace
* the placeholders with the existing quote's values
*/
$output = $modx->getChunk('quotationTpl',$quote->toArray());
}
return $output;
I then changed this to:
<?php
$path = MODX_CORE_PATH . 'components/quotes/';
$result = $modx->addPackage('quotes',$path . 'model/','bobs_');
if (! $result) {
return 'failed to add package';
} else {
$output = '<p>added Package</p>';
}
$quotes = $modx->getCollection('Quotation');
/* Get the existing Quote */
$quoteId = $scriptProperties['quoteId'];
$quote = $modx->getObject('Quotation', array('id'=>$quoteId));
/* Show error message if quote is not found */
if (empty($quote)) {
return ('Could not find Quote with ID: ' . $quoteId);
}
if (isset($_POST['submit']) && $_POST['submit'] == 'MySubmitVar') {
/* Form has been submitted */
$quote->set('topic', $_POST['topic']);
$quote->set('quote', $_POST['quote']);
$quote->set('author', $_POST['author']);
if ( $quote->save()) {
$output = "Quote Created";
} else {
$output = "Error";
}
} else {
/* Not a repost, just display the form */
/* The second argument to getChunk() tells MODx to replace
* the placeholders with the existing quote's values
*/
$output = $modx->getChunk('quotationTpl',$quote->toArray());
}
return $output;
Which successfully outputs the quote which I can edit but it won't submit and save the change - where is this going wrong? And how would I potentially show all the quotes too?
Great tutorial, just need this last bit so that I can start building my own custom tables.
Many thanks in advance!