I am trying to create a custom model to interact with a custom database table using xPDO. To do this I have been following Bob Ray’s guide Bob's Guides | Custom DB Tables.
I have created the table, and using the snippet provided by Bob to generate the package and the schema and class files which all appears to work fine. The generated directory and file structure of the looks like this below:
When I run addPackage on the new quotes package it works fine, however, whenever I run any kind of xPDO function to try and get the data from the custom database table I encounter the errors below.
[2023-02-07 12:30:36] (ERROR @ /home/wetrak/public_html/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 666) Could not load class: Quotation from mysql.quotation
[2023-02-07 12:30:36] (ERROR @ /home/wetrak/public_html/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 786) Quotation::loadCollection() is not a valid static method.
[2023-02-07 12:30:36] (ERROR @ /home/wetrak/public_html/core/cache/includes/elements/modx/revolution/modsnippet/98.include.cache.php : 14) PHP warning: count(): Parameter must be an array or an object that implements Countable
[2023-02-07 12:30:36] (ERROR @ /home/wetrak/public_html/core/cache/includes/elements/modx/revolution/modsnippet/98.include.cache.php : 16) PHP warning: Invalid argument supplied for foreach()
I have tried creating a custom model using MIGX, ClassExtender and ExtraBuilder but always end up with the same error messages.
I am on MODX 3.0.0-pl - PHP 7.2 - MySQL 5.7.41
The contents of the /core/components/quotes/model/quotes/Quotation.php:
[+class-header+]
[+phpdoc-start+]
[+phpdoc-properties+]
[+phpdoc-end+]
[+class-declaration+]
[+class-traits+][+class-constants+][+class-properties+][+class-methods+][+class-close-declaration+][+class-footer+]
The contents of the /core/components/quotes/model/quotes/metadata.mysql.php:
<?php
$xpdo_meta_map = array (
'version' => '3.0',
'namespace' => 'quotes',
'namespacePrefix' => '',
'class_map' =>
array (
'xPDO\\Om\\xPDOSimpleObject' =>
array (
0 => 'quotes\\Quotation',
),
),
);
The contents of the /core/components/quotes/model/quotes/mysql/Quotation.php:
[+class-header+]
[+class-declaration+]
[+class-traits+][+class-constants+]
public static $metaMap = array (
'package' => 'quotes',
'version' => '3.0',
'table' => 'quotation',
'extends' => 'xPDO\\Om\\xPDOSimpleObject',
'tableMeta' =>
array (
'engine' => 'MyISAM',
),
'fields' =>
array (
'quote' => NULL,
'author' => '',
'topic' => '',
),
'fieldMeta' =>
array (
'quote' =>
array (
'dbtype' => 'mediumtext',
'phptype' => 'string',
'null' => false,
'index' => 'index',
),
'author' =>
array (
'dbtype' => 'varchar',
'precision' => '200',
'phptype' => 'string',
'null' => false,
'default' => '',
'index' => 'index',
),
'topic' =>
array (
'dbtype' => 'varchar',
'precision' => '20',
'phptype' => 'string',
'null' => false,
'default' => '',
'index' => 'index',
),
),
'indexes' =>
array (
'topic' =>
array (
'alias' => 'topic',
'primary' => false,
'unique' => false,
'type' => 'BTREE',
'columns' =>
array (
'topic' =>
array (
'length' => '',
'collation' => 'A',
'null' => false,
),
),
),
'author' =>
array (
'alias' => 'author',
'primary' => false,
'unique' => false,
'type' => 'BTREE',
'columns' =>
array (
'author' =>
array (
'length' => '',
'collation' => 'A',
'null' => false,
),
),
),
'quote' =>
array (
'alias' => 'quote',
'primary' => false,
'unique' => false,
'type' => 'FULLTEXT',
'columns' =>
array (
'quote' =>
array (
'length' => '',
'collation' => '',
'null' => false,
),
'author' =>
array (
'length' => '',
'collation' => '',
'null' => false,
),
'topic' =>
array (
'length' => '',
'collation' => '',
'null' => false,
),
),
),
),
);
[+class-properties+][+class-methods+][+class-close-declaration+][+class-footer+]
The contents of the /core/components/quotes/schema/quotes.mysql.schema.xml:
<?xml version="1.0" encoding="UTF-8"?>
<model package="quotes" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="MyISAM" version="3.0">
<object class="Quotation" table="quotation" extends="xPDO\Om\xPDOSimpleObject">
<field key="quote" dbtype="mediumtext" phptype="string" null="false" index="index" />
<field key="author" dbtype="varchar" precision="200" phptype="string" null="false" default="" index="index" />
<field key="topic" dbtype="varchar" precision="20" phptype="string" null="false" default="" index="index" />
<index alias="topic" name="topic" primary="false" unique="false" type="BTREE" >
<column key="topic" length="" collation="A" null="false" />
</index>
<index alias="author" name="author" primary="false" unique="false" type="BTREE" >
<column key="author" length="" collation="A" null="false" />
</index>
<index alias="quote" name="quote" primary="false" unique="false" type="FULLTEXT" >
<column key="quote" length="" collation="" null="false" />
<column key="author" length="" collation="" null="false" />
<column key="topic" length="" collation="" null="false" />
</index>
</object>
</model>
Thanks I’ve been struggling with this for a while.