0

我制作了一个非常简单的代码示例。这不是作为命名空间添加到 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 看到我创建的任何模型。完全没有。

任何想法将不胜感激!谢谢,

4

3 回答 3

2

Modx/xPDO 在路径上带有斜杠……您是否尝试过在您的 .../path/to/model/ 中添加斜杠?

这个“设计决定”让我很多次

于 2013-11-08T22:13:47.543 回答
0

在数据库中,createObjectContainer 之后的表有吗?如果是,并且文件都是正确的,我只能想象您添加的模型是错误的。

于 2013-11-08T22:03:42.607 回答
0

assets/components/package 用于网站的正面方面。core/components/package 用于后端脚本和包。

一个包可以有两种目录结构,也可以只有其中一种。

您只需要创建一个调用该类的片段,运行 initalize 并完成。

或者,您可以创建一个插件以将类包含在 MODX 服务中,并将其设置为在 onWebPageInit 事件上运行。

http://www.shawnwilkerson.com/xpdo/2014/11/28/sample-class-for-use-with-xpdo/

于 2015-08-09T21:02:45.927 回答