我正在尝试为10月CMS制作一个从2个XML文件中获取的插件,我想知道构建器插件是否适合生成所需的文件来完成此任务,如果是这样。是我需要更改的模型文件,以便可以连接到XLM文件以获取所需的数据
我沿着这个线路
<?php namespace XmlXmldataModels;
use BackendModelsUser;
use CmsClassesPage;
use File;
use Flash;
use Hash;
use Markdown;
use Model;
use OctoberRainSupportValidationException;
use Storage;
use Str;
use SystemClassesPluginManager;
use SystemModelsFile as FileModel;
use XmlXmldataXmlloadfile;
/**
* XML File Model
*/
class Xmldata extends Model
{
$xmldata = simplexml_load_file("../storage/testdata.xml")
$keypairdata1 = "";
$keypairdata2 = "";
for ($i = 0; $i < count($xmldata); $i++){
$keypairdata1 = $xmldata->testdata[$i]->keypairdata1;
$keypairdata2 = $xmldata->testdata[$i]->keypairdata2;
}
testdata xml文件
<MYData>
<login_details>
<unique_ref>1-61</unique_ref>
<login_name>tomme</login_name>
<login>me</login>
<password>me</password>
<file1>Test</file1>
<file2/>
<file3/>
<file4/>
</login_details>
</MYData
这是客户端数据文件
<Mydata>
<client-data>
<refno_con>63</refno_con>
<details>Picture No 14</details>
<stat_date>2011-10-04</stat_date>
<val_amount>460.00</val_amount>
<stat_file>Z:DATA\documentsLanscape.jpg</stat_file>
<unique_ref>1-63</unique_ref>
</client-data>
</Mydata>
我要添加评论,但这很容易,我全都适合简单的路线,
<?
use RainlabUserModelsUser;
function onStart() {
// Anonymous Class only working on PHP7
$this['code'] = new class {
public function data() {
$path = themes_path('path to clients in themes directory /xml/data.xml');
$numPerPage = 12; // max. number of items to display per page
$xml = simplexml_load_file($path);
$refno_con = Auth::getUser()->refno_con;// you will need to add this field to your users table to reflect the values in you clients data file
$data = $xml->xpath('/Mydata/client-data[refno_con="' . $refno_con . '"]');
$details = json_decode(json_encode($data), TRUE);
krsort($details); // sorts an associative array in descending order, according to the key
//ksort($details); //sorts an associative array in ascending order, according to the key
$pagedArray = array_chunk($details, $numPerPage, true);
$nthPage = $pagedArray['1'];
return $nthPage;
}
};
}
?>
这应该做工作
如果您想要一个类似模型的接口与XML文件交互,那么最好的选择是使用随附的Halcyon(elo句(库。请参阅https://github.com/octobercms/october/blob/wip/halcyon-db-datasource/modules/modules/cms/classes/meta.php,以示例存储YAML文件内部数据的Halcyon模型示例。您可以将其用作弄清楚如何为XML文件做相同的起点。
我一直在考虑自己做这样的事情,但没有时间,但是找到了一件有趣的代码,您可能想尝试而不是试图重塑车轮称为ante laca创建了一个名为xmldb的脚本,您可以通过搜索他的名字来找到它,或者是链接https://github.com/alaca/xmldb到github上的文件,它得到了我所理解的完整crud支持,让我知道如何你上了
我一直在研究这个问题,过去我已经为客户网站
做了这一点<?php
use RainlabUserModelsUser;
function onStart() {
// Anonymous Class only working on PHP7
$this['code'] = new class {
public function data() {
$numPerPage = 12; // max. number of items to display per page
$data = range(1, 150); // data array to be paginated
$num_results = count($data);
$xml = simplexml_load_file("http://to your xml file");
$users = User::whereIsActivated(true)->get();
foreach ($users as $user) {
$refno_con = $user->refno_con;
$data = $xml->xpath('/Mydata/client-data[refno_con="' . $refno_con . '"]');
$details = json_decode(json_encode($data), TRUE);
krsort($details); // sorts an associative array in descending order, according to the key
//ksort($details); //sorts an associative array in ascending order, according to the key
$pagedArray = array_chunk($details, $numPerPage, true);
$nthPage = $pagedArray['1'];
return $nthPage;
}
}
};
}
?>
并将其放在页面的"代码"部分中,然后使用树枝访问此
{# in twig Markup #}
<ul>
{% for item in code.data %}
<li>{{ item.details }}</li>
<li>{{ item.stat_date }}</li>
<li>{{ item.refno_con }}</li>
{% endfor %}
</ul>
您需要使用用户插件并修改它以适合您的需求,我使用refno_con在示例文件中迭代客户详细信息,希望这有助于