我制作了一个非常简单的代码示例。这不是作为NameSpace添加到Modx中的。从空白的Modx安装开始。
创建方案文件:
<?xml version="1.0" encoding="UTF-8"?>
<model package="fwhisky" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
<object class="FWhiskyBrand" table="fwhisky_brand" extends="xPDOSimpleObject">
<field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
</object>
<object class="FWhiskyExpression" table="fwhisky_expression" extends="xPDOSimpleObject">
<field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
</object>
</model>
运行生成器:
<?php
require_once dirname(__FILE__).'/build.config.php';
include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx= new modX();
$modx->initialize('mgr');
$modx->loadClass('transport.modPackageBuilder','',false, true);
$modx->setLogLevel(modX::LOG_LEVEL_INFO);
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
$basePath = $modx->getOption('fwhisky.core_path',null,$modx->getOption('core_path').'components/fwhisky/');
$sources = array(
'model' => $basePath.'model/',
'schema_file' => $basePath.'model/schema/fwhisky.mysql.schema.xml'
);
$manager= $modx->getManager();
$generator= $manager->getGenerator();
if (!is_dir($sources['model'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Model directory not found!'); die(); }
if (!file_exists($sources['schema_file'])) { $modx->log(modX::LOG_LEVEL_ERROR,'Schema file not found!'); die(); }
$generator->parseSchema($sources['schema_file'],$sources['model']);
$modx->addPackage('fwhisky', $sources['model']); // add package to make all models available
$manager->createObjectContainer('FWhiskyBrand'); // created the database table
$manager->createObjectContainer('FWhiskyExpression'); // created the database table
$modx->log(modX::LOG_LEVEL_INFO, 'Done!');
这会构建模型文件:
<?php
class FWhiskyBrand extends xPDOSimpleObject {}
。
<?php
$xpdo_meta_map = array (
'xPDOSimpleObject' =>
array (
0 => 'FWhiskyBrand',
1 => 'FWhiskyExpression',
),
);
<?php
require_once (dirname(dirname(__FILE__)) . '/fwhiskybrand.class.php');
class FWhiskyBrand_mysql extends FWhiskyBrand {}
<?php
$xpdo_meta_map['FWhiskyBrand']= array (
'package' => 'fwhisky',
'version' => NULL,
'table' => 'fwhisky_brand',
'extends' => 'xPDOSimpleObject',
'fields' =>
array (
'title' => '',
),
'fieldMeta' =>
array (
'title' =>
array (
'dbtype' => 'varchar',
'precision' => '255',
'phptype' => 'string',
'null' => false,
'default' => '',
),
),
);
<?php
require_once (dirname(dirname(__FILE__)) . '/fwhiskyexpression.class.php');
class FWhiskyExpression_mysql extends FWhiskyExpression {}
<?php
$xpdo_meta_map['FWhiskyExpression']= array (
'package' => 'fwhisky',
'version' => NULL,
'table' => 'fwhisky_expression',
'extends' => 'xPDOSimpleObject',
'fields' =>
array (
'title' => '',
),
'fieldMeta' =>
array (
'title' =>
array (
'dbtype' => 'varchar',
'precision' => '255',
'phptype' => 'string',
'null' => false,
'default' => '',
),
),
);
core/components/fwhisky/
core/components/fwhisky/model
core/components/fwhisky/model/fwhisky
core/components/fwhisky/model/fwhisky/mysql
core/components/fwhisky/model/fwhisky/mysql/fwhiskyexpression.class.php
core/components/fwhisky/model/fwhisky/mysql/fwhiskyexpression.map.inc.php
core/components/fwhisky/model/fwhisky/mysql/fwhiskybrand.map.inc.php
core/components/fwhisky/model/fwhisky/mysql/fwhiskybrand.class.php
core/components/fwhisky/model/fwhisky/fwhiskyexpression.class.php
core/components/fwhisky/model/fwhisky/metadata.mysql.php
core/components/fwhisky/model/fwhisky/fwhiskybrand.class.php
core/components/fwhisky/model/schema
core/components/fwhisky/model/schema/fwhisky.mysql.schema.xml
只是为了偏执,我检查了以上所有文件都是世界可读的!
然后,让我们创建一个简单的测试页面:
<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
require_once MODX_CONNECTORS_PATH.'index.php';
$corePath = $modx->getOption('fwhisky.core_path',null,$modx->getOption('core_path').'components/fwhisky/');
if (!$modx->addPackage('fwhisky', $corePath."model") ) {
print "CANT ADD";
die("CANT ADD");
}
print "Path: ".$corePath."model"."<p>";
$class = "FWhiskyBrand";
$c = $modx->newQuery($class);
$brands = $modx->getCollection($class,$c);
print "End";
当我们运行它时,我们得到
Path: /home/james/modx/core/components/fwhisky/model
End
我检查了那条路......
james@debian:~/modx$ ls -al /home/james/modx/core/components/fwhisky/model
total 16
drwxr-xr-x 4 james james 4096 Nov 8 10:04 .
drwxr-xr-x 3 james james 4096 Nov 8 10:21 ..
drwxr-xr-x 3 james james 4096 Nov 8 10:04 fwhisky
drwxr-xr-x 2 james james 4096 Nov 8 09:28 schema
但是现在,我们检查错误日志:
[2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) Could not load class: FWhiskyBrand from mysql.fwhiskybrand.
[2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) Could not load class: FWhiskyBrand from mysql.fwhiskybrand.
[2013-11-08 10:32:31] (ERROR @ /assets/components/fwhisky/connector.php) FWhiskyBrand::loadCollection() is not a valid static method.
所以基本上,我似乎无法让Modx看到我创建的任何模型。完全没有。
任何想法都将不胜感激!谢谢,
答案 0 :(得分:2)
Modx / xPDO在路径上有大量的尾随斜杠...你试过在你的... / path / to / model /中添加一个尾部斜杠吗?
这个“设计决定”让我感到很多次
答案 1 :(得分:0)
在数据库中,createObjectContainer之后的表是否存在?如果它们是,并且文件都是正确的,我只能想象你正在添加错误的模型。
答案 2 :(得分:0)
资产/组件/包是针对网站的前端方面。 核心/组件/包用于后端脚本和包。
包可以包含两个目录结构,也可以只包含其中一个。
您只需创建一个调用该类的代码段,运行initalize并完成。
或者,您可以创建一个插件以在MODX服务中包含该类,并将其设置为在onWebPageInit事件上运行。
http://www.shawnwilkerson.com/xpdo/2014/11/28/sample-class-for-use-with-xpdo/