$sql = 'SELECT * FROM orders ORDER BY created DESC LIMIT 1';
假設你的模式是一樣
<?xml version="1.0" encoding="UTF-8"?>
<model package="your_package_name" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Orders" table="orders" extends="xPDOSimpleObject">
<field key="order_no" dbtype="varchar" precision="255" phptype="string" null="false" />
<!-- and everything else below -->
</object>
<!-- and everything else below -->
</model>
你需要做的是如此簡單:
$modx->addPackage('your_package_name'); // Add your table schema to MODX object
$orders = $modx->getCollection('Orders'); // class's name, not table's name
$output = '';
if ($orders){
$outputArray = array();
foreach ($orders as $order) {
$orderArray = $order->toArray();
$outputArray[] = $modx->getChunk('your_row_chunk_name', $orderArray);
}
$wrapper = array(
'orders' => @implode("\n", $outputArray);
);
$output = $modx->getChunk('your_wrapper_chunk_name', $wrapper);
}
return $output;
這是從一個自定義的第三方組件?你是否在數據庫中爲這個字段創建了一個對象? – OptimusCrime
是的,是的,但我也需要它爲在modx中沒有對象的自定義數據庫表工作。我從這個網頁得到了這個:http://forums.modx.com/thread/35321/querying-a-non-modx-table-in-revolution – Meddie
嘗試用'PDO_FETCH_ASSOC'切換'PDO :: FETCH_ASSOC'?你有任何錯誤? – OptimusCrime