0

我正在尝试创建一个与 CSV 一起使用的数据源。我正在关注这个例子

连接.php:

Connections::add('csv', [
        'type' => 'file',
        'adapter' => 'Csv',
    ]
);

应用程序/扩展/适配器/数据/源/文件/Csv.php

namespace app\extensions\adapter\data\source\file;

use lithium\core\Libraries;

class Csv extends \app\extensions\adapter\data\source\File {

    public function __construct(array $config = []) {
        $defaults = [
            'delimiter' => ',',
            'enclosure' => '\"',
            'escape' => '\\',
            'path' => Libraries::get(true, 'resources') . '/file/csv',
        ];
        $config += $defaults;
        parent::__construct($config);
    }

    public function read($query, array $options = array()) {
        print_r($query);
        die();
    }
}

应用程序/扩展/适配器/数据/源/File.php

<?php

namespace app\extensions\adapter\data\source;

use lithium\core\Libraries;

class File extends \lithium\core\Object {
    public function __construct(array $config = array()) {
        $defaults = array(
            'encoding' => 'UTF-8',
            'path' => Libraries::get(true, 'resources') . '/file'
        );
        parent::__construct($config + $defaults);
    }
}

?>

应用程序/模型/Importers.php

<?php

namespace app\models;

class Importers extends \lithium\data\Model {
    protected $_meta = [ 
        'connection' => 'csv',
    ];
}
?>

每当我Importers::find('all');从控制器调用时,我都会收到错误消息: Fatal error: Call to undefined method app\extensions\adapter\data\source\file\Csv::configureClass() in ...

如果我确实定义configureClass()了,我会收到另一个错误说明enable()未定义。不太确定我在这里做错了什么。

4

1 回答 1

0

如果我错了,请纠正我,但似乎File必须扩展抽象类\lithium\data\Source,而抽象类又扩展了\lithium\core\Object

因此,app/extensions/adapter/data/source/File.php将如下所示:

class File extends \lithium\data\Source {

以下是 Source.php 的注释:

/**
 * This is the base class for Lithium's data abstraction layer.
 *
 * In addition to utility methods and standardized properties, it defines the implementation tasks
 * for all Lithium classes that work with external data, such as connections to remote resources
 * (`connect()` and `disconnect()`), introspecting available data objects (`sources()` and
 * `describe()`), and a standard read/write interface (`create()`, `read()`, `update()` and
 * `delete()`).
 *
 * Subclasses may implement any other non-standard functionality, but the above methods define the
 * requirements for interacting with `Model` objects, and other classes within `lithium\data`.
 */

希望对某人有所帮助(如果我没记错的话:))。我喜欢这个框架!

于 2013-06-19T09:27:19.453 回答