dongzhang7961
2010-11-23 12:27
采纳率: 0%
浏览 37

在datasource中实现CakePHP find()操作

I'm implementing a custom datasource in my CakePHP application, I've implemented the basic functions for a datasource (read(), listSources(), describe()). The datasource uses Xml as input and I would really like to use find('neighbors') on the Xml and was wondering if Cake "automagically" implements this feature (because the read() function is there), or if I need to extend the datasource somehow. I haven't found a specific example yet, so I'm hoping the SO community will be able to help.

Below is the current datasource implementation.

<?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;
 }
}
?>

Basic Xml structure:

<items>
 <item id="1">
   <type>Type</type>
   <title>Title</title>
   <subtitle>Subtitle</subtitle>
   <date>15-12-2010</date>
   <body>Body text</body>
 </item>
</items>

Edit:

Should've read the manual more carefully:

And that's pretty much all there is to it. By coupling this datasource to a model, you are then able to use Model::find()/save() as you would normally, and the appropriate data and/or parameters used to call those methods will be passed on to the datasource itself, where you can decide to implement whichever features you need (e.g. Model::find options such as 'conditions' parsing, 'limit' or even your own custom parameters).

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhe9075 2010-11-23 14:02
    已采纳

    I suspect that you'll have to show Cake how to find neighbours by defining a method in the datasource.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题