doudan5136 2013-11-08 16:14
浏览 22
已采纳

无法在Modx中创建和引用模型

I have produced a really simple code sample. This was not added as a NameSpace into Modx. Start with a blank Modx install.

Create scheme file:

<?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>

Run generater:

<?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!');

This builds the model files:

<?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

Just to be paranoid, I've checked all the above files are world readable!

Then, lets create a simple test page:

<?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";

When we run it we get

Path: /home/james/modx/core/components/fwhisky/model

End

I've checked that path ...

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

But now, we check the error log:

 [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.

So basically, I can't seem to make Modx see any models I create. At all.

Any ideas would be greatly appreciated! Thanks,

  • 写回答

3条回答 默认 最新

  • dongza3124 2013-11-08 22:13
    关注

    Modx/xPDO has a massive thing with trailing slashes on paths... have you tried adding a trailing slash to your .../path/to/model/ ?

    This 'design decision' has caught me out many a time

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?