实施CakePHP的find()方法中的数据源操作
-
27-09-2019 - |
题
我执行我的CakePHP程序的自定义数据源,我已经实现了基本功能的数据源(read()
,listSources()
,describe()
)。
数据源使用XML作为输入,我真的想使用找到XML(可“邻居”),并想知道如果蛋糕“自动地”实现了该功能(因为read()
功能是存在的),或者如果我需要扩展数据源不知何故。我还没有找到一个具体的例子呢,所以我希望多所社区就能帮助。
下面是当前的数据源实现。
<?php
App::import('Core', 'Xml');
class AppdataSource extends DataSource {
protected $_schema = array(
'apps' => array(
'id' => array(
'type' => 'integer',
'null' => true,
'key' => 'primary',
'length' => 11,
),
'type' => array(
'type' => 'string',
'null' => true,
'length' => 140
),
'title' => array(
'type' => 'string',
'null' => true,
'length' => 255
),
'subtitle' => array(
'type' => 'string',
'null' => true,
'length' => 255
),
'body' => array(
'type' => 'text',
'null' => true,
),
'date' => array(
'type' => 'date',
'null' => true,
),
)
);
public function listSources() {
return array('apps');
}
public function describe($model) {
return $this->_schema['apps'];
}
function calculate(&$model, $func, $params = array()) {
return '__'.$func;
}
function __getPage($items = null, $queryData = array()) {
if (empty($queryData['limit']) ) {
return $items;
}
$limit = $queryData['limit'];
$page = $queryData['page'];
$offset = $limit * ($page-1);
return array_slice($items, $offset, $limit);
}
function __sortItems(&$model, $items, $order) {
if ( empty($order) || empty($order[0]) ) {
return $items;
}
$sorting = array();
foreach( $order as $orderItem ) {
if ( is_string($orderItem) ) {
$field = $orderItem;
$direction = 'asc';
}
else {
foreach( $orderItem as $field => $direction ) {
continue;
}
}
$field = str_replace($model->alias.'.', '', $field);
$values = Set::extract($items, '{n}.'.$field);
if ( in_array($field, array('lastBuildDate', 'pubDate')) ) {
foreach($values as $i => $value) {
$values[$i] = strtotime($value);
}
}
$sorting[] = $values;
switch(low($direction)) {
case 'asc':
$direction = SORT_ASC;
break;
case 'desc':
$direction = SORT_DESC;
break;
default:
trigger_error('Invalid sorting direction '. low($direction));
}
$sorting[] = $direction;
}
$sorting[] = &$items;
$sorting[] = $direction;
call_user_func_array('array_multisort', $sorting);
return $items;
}
public function read($model, $queryData = array()) {
$feedPath = 'xml/example.xml';
$xml = new Xml($feedPath);
$xml = $xml->toArray();
foreach ($xml['Items']['Item'] as $record) {
$record = array('App' => $record);
$results[] = $record;
}
$results = $this->__getPage($results, $queryData);
//Return item count
if (Set::extract($queryData, 'fields') == '__count' ) {
return array(array($model->alias => array('count' => count($results))));
}
return $results;
}
}
?>
基本XML结构:
<items>
<item id="1">
<type>Type</type>
<title>Title</title>
<subtitle>Subtitle</subtitle>
<date>15-12-2010</date>
<body>Body text</body>
</item>
</items>
修改强>
应该已经阅读本说明书更仔细地:
和这几乎是所有有 它。通过这个数据源的连接 模型,你就能够使用 型号::发现()/保存(),你会 通常,和适当的数据 和使用/或参数来调用那些 方法将被传递到 数据源本身,在那里你可以 决定执行哪个功能 你需要(例如型号::查找选项 如“条件”的解析,“限制” 甚至自己的自定义参数)。
解决方案
我怀疑你必须的显示的蛋糕怎么找到数据源中定义的方法的邻居。
不隶属于 StackOverflow