Topic: Needing More Examples for xPDO database interaction  (Read 1123 times)

Pages: [1]   Go Down

#1: 31-Oct-2009, 05:58 PM


Everett
Posts: 883

WWW
I finally took a serious whack at the procedure outlined in this article:

http://svn.modxcms.com/docs/display/revolution/Using+Custom+Database+Tables+in+your+3rd+Party+Components

But it left me seriously confused.  Has anyone gotten this working?

Specifically, I'd like to see that article simplified into component parts so it doesn't make my head explode.  I recommend the following topics (in this order)

1. Once I've got an XML schema definition, how do I use it in my Snippets to access rows?  E.g. MODx already has a bunch of XML files for accessing its own tables... what I'd love to see is how to use the xPDO API to get data from those tables.

2. Making an xPDO XML schema definition file to match an existing table:
    a) show how to build the XML file manually, using existing files in /core/model/schema as starting points.  I think this is handled pretty well here: http://svn.modxcms.com/docs/display/xPDO20/Defining+the+Database+and+Tables
   b) show how to use xPDO to automatically create the XML (i.e. reverse engineer the schema of an existing table).  I've looked at the script posted here http://modxcms.com/forums/index.php?topic=40174.0 but I don't have any idea what to do with the output. I see that the schema file WAS generated in /core/model/schema, but more explanation is probably needed.  One note is that this script needs to be run as a web page (command line execution fails), e.g. http://domain.com/core/components/xyz/reverse_engineer.php  More importantly, how do I use my schema file once I've got it? (see below)

3. Using MODx and xPDO to create a new MySQL table based off a new schema definition.



« Last Edit: 31-Oct-2009, 06:09 PM by Everett »

#2: 31-Oct-2009, 09:59 PM

Marketing & Design Team

lossendae
Posts: 347

Modx addicted

WWW
Hello Everett,

Did you took at the three How to's on Revolution docs?
http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+I
http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+II
http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+III

There are a lot of examples on how to interact with your database.

#3: 1-Nov-2009, 08:01 PM


Everett
Posts: 883

WWW
Thanks, those articles are good... but unfortunately they only cover the built-in objects (i.e. MODx internals).  The bit I'm looking for is how to interact with my own custom objects... but that's not covered.  The promising quote:
Quote
You can also define your own schemas for your own components and add them as packages - more on that in a future article.

I want to get things off the ground interacting with my own custom tables.  If I could see a couple examples, I could get this going... 

Thanks!

#4: 1-Nov-2009, 08:28 PM

Foundation

splittingred
Posts: 1,515

i am alt-country rock

WWW
Thanks, those articles are good... but unfortunately they only cover the built-in objects (i.e. MODx internals).  The bit I'm looking for is how to interact with my own custom objects... but that's not covered.  The promising quote:
Quote
You can also define your own schemas for your own components and add them as packages - more on that in a future article.

I want to get things off the ground interacting with my own custom tables.  If I could see a couple examples, I could get this going... 

Thanks!

Everett,

So, I'm assuming you've built your model maps and classes, you'll just run this function:

Code:
$modx->addPackage('mypackagename','/path/to/my/model/');

// ie, for a package "test" in your "core/components/myapp/model/test/'
$modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');

This will add all the maps to the current xPDO instance so that it can grab the object definitions. You can then grab your classes (table objects) by doing:

Code:
$modx->getObject('test.myObject');

// or, alternatively, but a tiny bit slower, because it has to search for the package:
$modx->getObject('myObject');

Feel free to ask further questions!
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#5: 2-Nov-2009, 12:14 AM

Coding Team

BobRay
Posts: 5,356

WWW
@splittingred: Now you've confused me.  Tongue

How does loadClass() fit into this picture (assuming that it does)?

I would have thought you'd have to load a class before you could uses getObject(), new, or newObject() on it.

It think it would help if I knew what or where the class and package are being loaded or added *to*.

Part of my confusion stems from the fact that captcha.php has this code in it (I think you know how it got there Wink ):

Code:
$modx->addPackage('captcha',$captcha_core_path.'model/');
$modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);

Is one or the other of these lines not needed or do they do different things?

(I hope this isn't hijacking the thread -- I'm confused enough that I can't tell).
MODx info for newbies: http://bobsguides.com/MODx.html

#6: 2-Nov-2009, 02:55 AM

Marketing & Design Team

lossendae
Posts: 347

Modx addicted

WWW
Hello BobRay,

Here is what i understand from you're previous post:

Code:
<?php
//This line load the package captcha created with build.transport.php
//If there are maps file located in the model directory, they will be loaded too so you can have access
//To your table with xPDO.
$modx->addPackage('captcha',$captcha_core_path.'model/');

//This line load the class called veriword.class.php located in the model directory
//This is not directly related to xPDO.
$modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);

The maps files that you can create will be located in the model directory.
When you load the package you can also precise the prefix of your custom table with the third option:

Code:
<?php
$this
->modx->addPackage('yourpackage',$this->config['model_path'],'yourprefix_');


#7: 2-Nov-2009, 08:19 AM

Foundation

splittingred
Posts: 1,515

i am alt-country rock

WWW
How does loadClass() fit into this picture (assuming that it does)?
I would have thought you'd have to load a class before you could uses getObject(), new, or newObject() on it.
It think it would help if I knew what or where the class and package are being loaded or added *to*.
Part of my confusion stems from the fact that captcha.php has this code in it (I think you know how it got there Wink ):
Code:
$modx->addPackage('captcha',$captcha_core_path.'model/');
$modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);
Is one or the other of these lines not needed or do they do different things?
(I hope this isn't hijacking the thread -- I'm confused enough that I can't tell).
Nope, here, loadClass specifies that it's a transient class - ie, it's not directly related to a DB table. So we use loadClass, rather than newObject, because it's not loading an xPDOObject-based object - just a transient, helper class.

newObject allows you to load xPDOObject-based objects (or custom-loader based objects, but that's a whole 'nother topic) from the package maps and classes. It's not used for transient classes, or standalone classes, such as above.
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#8: 2-Nov-2009, 09:50 AM


Everett
Posts: 883

WWW
Thanks for the input.  Question: are you considering the XML schema your model?  Could you please give an exact example?  I'm sorry I'm so dense, but my mind can imagine that command pointing to several locations and working in several ways...

Does the following example load an XML file in /core/model/schema/?  e.g. /core/model/schema/mypackagename.mysql.schema.xml?  Or is it targeting the PHP files that reference that XML file?
Code:
$modx->addPackage('mypackagename','/path/to/my/model/');

If it's referencing the XML, is it assumed that the format of the file name is "mypackagename.mysql.schema.xml"?  If so, how do you change the database component (e.g. to Oracle or Mongo DB)?

And:
Code:
// ie, for a package "test" in your "core/components/myapp/model/test/'
$modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');

Again, does that command load up a PHP or an XML file? 

#9: 2-Nov-2009, 09:56 AM

Foundation

splittingred
Posts: 1,515

i am alt-country rock

WWW
Thanks for the input.  Question: are you considering the XML schema your model?  Could you please give an exact example?  I'm sorry I'm so dense, but my mind can imagine that command pointing to several locations and working in several ways...
Well first you'll need to parse the XML file and generate the object maps and class files:
http://svn.modxcms.com/docs/display/xPDO20/Generating+the+Model+Code

Quote
Does the following example load an XML file in /core/model/schema/?  e.g. /core/model/schema/mypackagename.mysql.schema.xml?  Or is it targeting the PHP files that reference that XML file?
Code:
$modx->addPackage('mypackagename','/path/to/my/model/');
It's targeting the PHP classes and maps.

Quote
If it's referencing the XML, is it assumed that the format of the file name is "mypackagename.mysql.schema.xml"?  If so, how do you change the database component (e.g. to Oracle or Mongo DB)?
It can technically be whatever you want - we've just standardized that so people dont get confused.

xPDO at this current time doesnt support other DB platforms; it will in the future. Once we get that up and running, we'll provide more info on how to do that.


Quote
Code:
// ie, for a package "test" in your "core/components/myapp/model/test/'
$modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');
Again, does that command load up a PHP or an XML file? 
It parses the PHP maps and classes.
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#10: 2-Nov-2009, 09:58 AM


Everett
Posts: 883

WWW
Thanks Shaun, I'll work with this.  Any ideas why the # of Confluence users has maxed out?  I wanted to add a couple clarifications to the articles about this.

#11: 2-Nov-2009, 10:01 AM

Foundation

splittingred
Posts: 1,515

i am alt-country rock

WWW
Thanks Shaun, I'll work with this.  Any ideas why the # of Confluence users has maxed out?  I wanted to add a couple clarifications to the articles about this.
Something very bizarre with our Confluence license. We're working on it at the moment. It's annoying, to say the least.
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#12: 2-Nov-2009, 10:08 AM

Foundation

splittingred
Posts: 1,515

i am alt-country rock

WWW
Okay, Confluence should be good now.
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.