phpmodxxpdo

Can't create and reference Model in 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,


Solution

  • 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