markcell/salvaon

基于 Eloquent 的 Laravel XML 文件管理包。

v1.0.6 2014-10-21 13:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:34:09 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

基于 Eloquent 的 Laravel XML 文件管理包,使用 SimpleXMLElement 和 xpath。

用法

通过 Composer 安装此包。

{
    "require": {
        "laravel/framework": "4.2.*",
        "markcell/salvaon": "dev-master" // or "markcell/salvaon": "1.0.*"
    }
}

编辑 'app/config/app.php' 文件,并在 'aliases' 数组中添加新项

'Salvaon' => 'Markcell\Salvaon\Salvaon'

从包中发布配置文件

php artisan config:publish markcell/salvaon

现在,您可以在 'app/config/packages/markcell/salvaon/config.php' 中编辑这些选项。或者将此文件复制到 'app/config' 文件夹中,文件名为 'salvaon.php'。

现在,您的 XML 模型可以简单地扩展 'Salvaon'

<?php

class Breakfast extends Salvaon {

    /**
     * The file associated with the model
     *
     * @var string
     */
    protected $file = 'breakfast.xml';
   
    /**
     * Root element of the document
     *  
     * @var string
     */
    protected $root = 'breakfast';  
 
    /**
     * Child elements of the root
     * 
     * @var string 
     */
    protected $child = 'food';     
 
    /**
     * The primary key for the model
     *
     * @var string
     */
    protected $primaryKey = 'name';

}

有关 'breakfast.xml' 文件示例的链接:https://github.com/markcell/salvaon/blob/master/breakfast.xml

示例

// Get all child nodes from XML root.
$foods = Breakfast::all();


// Count elements from selected childs $foods.
$foods->count();


// Get child from XML by $primaryKey or fail if not exists.
Breakfast::findOrFail('French Toast');


// Get child from XML with where condition.
$food = Breakfast::select()->where('name', '=', 'French Toast')->get();

// Update fields from selected child $food. 
$food->price = '3.25€';
$food->calories = 450;

// Save changes.
$food->save();


// Create new XML child.
$new = new Breakfast;

// Add data to child fields.
$new->name = 'French Toast';
$new->price = '4.50€';
$new->description = 'Thick slices made from our homemade sourdough bread';
$new->calories = 600;

// Save new child to XML file with tag attributes.
$new->save(array('id' => 26092014));

许可证

MIT 许可证下授权。