[MODx] 7. MIGX DB

MODx provides a really unfriendly way to work with xPDO class. What I means is you need to define XML schma, mysql table and run the script to generate other stuff (model and controller), which is really suck...

Luckly, MIGX DB could help you finish those task easily.

 

1. Install MIGX package.

2. On the topp menu, find "Extras" => "MIGX".

3. Enter into the MIGX Management, fill in the package and prefix information:

Then click "Create Package" button.

4. Go to "XML Schema" tab, define the schea here. 

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<model package="storefinder" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="storefinder" phpdoc-subpackage="model" version="1.1">
  <object class="sfStore" table="sfinder_stores" extends="xPDOSimpleObject">
    <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
    <field key="address" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    <field key="city" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    <field key="state" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    <field key="zip" dbtype="varchar" precision="10" phptype="string" null="false" default="0" index="index" />
    <field key="country" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
    <field key="phone" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
    <field key="fax" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
    <field key="active" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
    <alias key="postalcode" field="zip" />
    <index alias="name" name="name" primary="false" unique="false" type="BTREE">
        <column key="name" length="" collation="A" null="false" />
    </index>
    <index alias="zip" name="zip" primary="false" unique="false" type="BTREE">
        <column key="zip" length="" collation="A" null="false" />
    </index>
  </object>
  <object class="sfOwner" table="sfinder_owners" extends="xPDOSimpleObject">
    <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
    <field key="email" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    <index alias="name" name="name" primary="false" unique="false" type="BTREE">
        <column key="name" length="" collation="A" null="false" />
    </index>
  </object>
  <object class="sfStoreOwner" table="sfinder_store_owners" extends="xPDOSimpleObject">
    <field key="store" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    <field key="owner" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
    <index alias="store" name="store" primary="false" unique="false" type="BTREE">
        <column key="store" length="" collation="A" null="false" />
    </index>
    <index alias="owner" name="owner" primary="false" unique="false" type="BTREE">
        <column key="owner" length="" collation="A" null="false" />
    </index>
  </object>
</model>
复制代码

 

I just copy the xml from here.

 

5. Click "Save Schema". 

6. Go to "Parse schema" tab to click "Parse schema" button, this will generate model and controller class.php.

7. Go to Create Table tab, click "Create Table" button. This will create mysql table for your according to your xml.

 

8. Just mock some dump data into the table and test it out. For example, we add two rows of data into modx_sfinder_stores table.

create a snippet called "store".

复制代码
<?php
$path = MODX_CORE_PATH . 'components/storefinder/';
$result = $modx->addPackage('storefinder',$path .
    'model/','modx_');


$storefinder = $modx->newObject('sfStore');
$storefinder->set('name', "XOPO");
$storefinder->set('address', "Somewhere in the world");
$storefinder->set('city', "Helsinki");
$storefinder->set('state', "north Karera");
$storefinder->set('zip', "88888");
$storefinder->set('country', "Finland");
$storefinder->set('phone', "987654");
$storefinder->set('fax', "0332");
$storefinder->set('active', "0");
$storefinder->save();
复制代码

9. Run the snippet should be able to add data into the database, then we can view the data.

复制代码
<?php
$path = MODX_CORE_PATH . 'components/storefinder/';
$result = $modx->addPackage('storefinder',$path .
    'model/','modx_');

$result= $modx->getCollection('sfStore');
$stores = array();

foreach($result as $res){

   echo $res->get('name');
   echo $res->get('address');        
}
复制代码

 

posted @   Zhentiw  阅读(3557)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示