我正在尝试创建一个与CSV配合使用的数据源。 我正在关注this example
connections.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()
未定义。
不太清楚我在这里做错了什么。
答案 0 :(得分: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`.
*/
希望能帮到某人(如果我没错的话:))。 我喜欢这个框架!