2010-11-23 4 views
0

CakePHP 애플리케이션에서 사용자 정의 데이터 소스를 구현 중이며 데이터 소스 (read(),,,, describe())의 기본 기능을 구현했습니다. 데이터 소스는 Xml을 입력으로 사용하며 Xml에서 find ('neighbors')를 사용하고 싶습니다. 케이크가 "자동으로"구현되면 (read() 함수가 있기 때문에)이 기능을 구현하는지 궁금 해서요. 어떻게 든 데이터 소스. 구체적인 예를 아직 찾지 못해서 SO 커뮤니티가 도와 줄 수 있기를 바랍니다.데이터 소스에서 CakePHP find() 작업 구현

아래는 현재 데이터 소스 구현입니다.

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

편집 :

더주의 깊게 설명서를 읽어해야했습니다 :

을 그리고 거의 모든 에가 있어요 그것. 모델이 데이터 소스를 결합하면 다음 /) (찾을 :: 모델을 사용하면 일반적으로 처럼) (저장할 수 있으며, 그 메소드를 호출하는 데 사용되는 적절한 데이터 및/또는 매개 변수가 전달됩니다 데이터 소스 자체에서 이 필요한 기능 (예 : Model :: find options , '조건'구문 분석, '제한' 또는 사용자 정의 매개 변수)을 구현할 수 있습니다.

답변

2

나는 어떻게 데이터 소스에 방법을 정의하여 이웃을 찾을 수 케이크를 보여주기 위해 당신이해야합니다 용의자.

+0

그래서 "automagic"이 없다면 ... :( – mensch

+2

아마도 나는 Cake가 해당 데이터 소스에서 이웃을 찾는 방법을 알고 있다면 데이터 소스가 이미 정의되어 있다고 가정합니다. – Leo

+0

당신이 걱정됩니다 맞습니다. 데이터 소스에 대한 매뉴얼을 더 자세히 읽어야합니다 (http://book.cakephp.org/view/848/Basic-API-For-DataSources). 원래 질문을 관련 정보로 업데이트했습니다. . – mensch

관련 문제