Topic: Noddy's guide to xPDO  (Read 12712 times)

Pages: [1] 2  All   Go Down

#1: 9-Mar-2007, 11:36 AM


nowhinjing
Posts: 29

Getting started with xPDO

"A guide by an idiot, for others of a similar persuasion ..... "

To set the scene, I use a Windows XP workstation and have a home server running SME Server 7.1 behind an IPCOP firewall connected to ADSL. The tools I use and recommend are noted at the end of this guide.

Having fallen in love with ModX, and having read up on the much anticipated new core coming in 0.9.7, I thought to myself, "i'd like to have a go at that", so I started to read up all I could find.

Disaster, I couldn't understand a word of it. Sorry Jason and Co. but you clever guys leave us mere mortals lagging far behind - especially this fat old fart in his late fifties whose last exposure to the world of programming involved COBOL.

So, having time on my hands after being made redundant (hooray!), I decided to see if I could delve into the mysterious box of tricks to see if I could produce a starters guide of such stunning simplicity even I could pretend to understand it. 99.9% of this content was originally created by Jason and others, all I have done is try to organise and present it in a simple manner.

So here goes :

1. Obtain a copy of xPDO from www.xpdo.org - either the packaged version as a zip file or the latest SVN using the tool of your choice.

2. Create a folder called /xpdo at the same level as /assets and /manager in your modx installation.

3. Create a folder called /_model under /xpdo and give it write permissions (777 using chmod or WinSCP).

4. Obtain a copy of the current modx095 xml schema (attached at the end of this post), and upload it to /_model.

5. Create a folder called /modx095 under /om and give it write permissions (777 using chmod or WinSCP).

6. In the manager, create a snippet (I called mine 'xml2class', but you please yourselves) as below :
Code:
<?php
///
// To forward-engineer your database, just tailor the [reverse-engineered]
// xml file (the schema) then comment the reverse-engineering line and
// uncomment the forward-engineering line; that will generate the classes
// and maps of your object model and provide the basic scaffolding for
// working with those objects
//
$mtimemicrotime();
$mtimeexplode(' '$mtime);
$mtime$mtime[1] + $mtime[0];
$tstart$mtime;
echo 
"</br>Execution started : </br>";

//
// set up and connect to the database
//
global $modx;
include_once ( 
$modx->config['base_path'] . 'xpdo/xpdo.class.php');

$xpdo= new xPDO('mysql:host=localhost;dbname=mdx''mdxAdmin''mdxAdminLouches''mdx_');
$xpdo->setPackage('modx095');
$xpdo->setDebug(true);

//
//  initiate the manager
//
$manager$xpdo->getManager();
$generator$manager->getGenerator();

//
//  reverse-engineer an existing database
//
//$xml= $generator->writeSchema(XPDO_CORE_PATH . '_model/mymodel.mysql.schema.xml', 'mymodel', 'xPDOObject', 'optional_tbl_prefix_');

//
//  forward-engineer a model
//
$generator->parseSchema(XPDO_CORE_PATH '_model/schema_modx095_mysql.xml');

$mtimemicrotime();
$mtimeexplode(" "$mtime);
$mtime$mtime[1] + $mtime[0];
$tend$mtime;
$totalTime= ($tend $tstart);
$totalTimesprintf("%2.4f s"$totalTime);
echo 
"</br>Execution ended : </br>";
echo 
"Execution time: {$totalTime}</br>";

?>



Now we can start. As I understand it and in very simple terms, xPDO needs an XML schema describing the tables and relationships within the ModX (or other) database from which it can build the classes and maps that enable it to access the data. (I can hear all the techies sniggering..).

There is some good news, and some good and not so good news.

The first good news is that Jason has very kindly provided the full schema for ModX 0.9.5 that you should have downloaded above, from which we can generate the required classes and maps using the snippet described above.

The second good news is that the same snippet can be used to reverse engineer an XML schema from an existing database BUT this will not contain the relationship information which will have to be added manually. In my view, being that we have a wonderful starting point with the modx095 schema, it would be wise not to try to reverse engineer but to manally update the existing shema and always forward engineer the classes and maps.

7. Create a new document in the manager called xPDO Generator; access Site Admin only, not rich text, using template (blank); with one line in it :
Code:
[! xml2class? !]


8. Log yourself in the front end as the SiteAdmin web user and execute your page from the recent pages list. Alternatively call your page explicitly using the code below (nn = number of the document created above):
Code:
http://yourserver/modx/index.php?id=nn


***I'm sure there is an easier way to do this - but it works - even if a bit convoluted!***

9. Now check in /om/modx095/mysql, you should have lots of classes and maps !

Now let's see if it works ......

10. In the manager, create a snippet (I called mine 'testPDO', but you please yourselves) as below :
Code:

<?php
//
//  get start time
//
$mtimemicrotime();
$mtimeexplode(' '$mtime);
$mtime$mtime[1] + $mtime[0];
$tstart$mtime;

echo 
"</br>Execution started: </br>";

//
//  startup and connect to the database
//
global $modx;
define('XPDO_MODE'2);
include_once ( 
$modx->config['base_path'] . 'xpdo/xpdo.class.php');

$xpdo= new xPDO('mysql:host=localhost;dbname=mdx''mdxAdmin''mdxAdminLouches''modx_');
$xpdo->setPackage('modx095');
$xpdo->setDebug(false);

//
//  get the record of your choice, here its the default content thank-you page, no. 46
//
$id46;
$criteria$xpdo->newQuery('modResource'$id);
$criteria->limit(1);
$docm$xpdo->getObject('modResource'$criteria);

//
//  get the column values that you want and display them
//
//$content= $docm->_fields['content']; ----- possible alternative get method
$content$docm->get('content');
$pagetitle$docm->get('pagetitle');
$longtitle$docm->get('longtitle');
echo 
"</br>Content   : {$content}";
echo 
"</br>PageTitle : {$pagetitle}</br>";

//
//  update the longtitle by adding a string
//
$longtitle2$longtitle '.added this phrase.';
$docm->set('longtitle'$longtitle2);
$docm->save();
echo 
"</br>Updated longtitle from {$longtitle} to {$longtitle2}</br>";

echo 
"</br>Execution ended: </br>";

$mtimemicrotime();
$mtimeexplode(' '$mtime);
$mtime$mtime[1] + $mtime[0];
$tend$mtime;
$totalTime= ($tend $tstart);
$totalTimesprintf("%2.4f s"$totalTime);

echo 
"</br>Execution time: {$totalTime}</br>";
?>



11. Create a new document in the manager called xPDO Test; access Site Admin only, not rich text, using template (blank); with one line in it :
Code:
[! testPDO? !]


12. Log yourself in the front end as the SiteAdmin web user and execute your page from the recent pages list, Alternatively call your page explicitly using the code below (nn = number of the document created above):
Code:
http://yourserver/modx/index.php?id=nn


... and that is the first step down the road...

The tools I use :
IPCOP Firewall   http://www.ipcop.org
SME SERVER   http://www.smeserver.org or http://www.contribs.org
Windows XP   ... no further comment
WinSCP                   http://www.winscp.net
Putty                   http://www.chiark.greenend.org.uk/~sgtatham/putty/
RapidSVN                   http://rapidsvn.tigris.org/


* schema_modx095_mysql.xml (44.1 KB - downloaded 312 times.)

#2: 9-Mar-2007, 11:55 AM

Foundation

splittingred
Posts: 1,520

i am alt-country rock

WWW
Great tutorial!

I wouldn't do too much with integrating into MODx though - 0.9.7 already has it integrated in a root directory called "core/".

Also, you can put the model/ directory anywhere - it doesn't have to be in om/ - by making sure when you call setPackage you specify the directory.
IE:
Code:
$xpdo->setPackage('mysite','/public_html/core/model/');
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#3: 9-Mar-2007, 03:59 PM


ChuckTrukk
Posts: 851

WWW
Is the 097 svn available anywhere yet?

Chuck
Chuck the Trukk
ProWebscape.com :: Nashville-WebDesign.com
- - - - - - - -
What are TV's? Here's some info below.
http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008

#4: 10-Mar-2007, 03:02 AM


nowhinjing
Posts: 29

@splitting red

The idea was to try out xPDO on something with which I was becoming familiar rather than try to write something new. It was basically an exercise to try to see how it all hangs together and works. I thought that it might also be useful for others going through the same learning curve.

I too am waiting with baited breath for 0.9.7 ! Then we can really start rolling on developing new features for ModX - but being totally unsure as to what will ship with the new release, I am loath to start on any heavy development. I had thought of re-writing NewsPublisher, would that be useful or would I be re-inventing the wheel ?

NWJ

#5: 10-Mar-2007, 03:51 PM

Foundation

rthrash
Posts: 11,353

WWW
Oh... newspublisher could use a bit of spiffing up!
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#6: 11-Mar-2007, 03:11 AM

Coding Team

sottwell
Posts: 10,530

WWW
I once got the NewsEditor part to add TVs when creating a new document, but never could figure out how to edit a TV for a given existing document! Essential for allowing front-end editing of a shopping catalog, for example.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#7: 11-Mar-2007, 06:38 AM


nowhinjing
Posts: 29

OK guys, I take a look at it - but no promises.

Another fine mess I've gotten myself into, Stanley.  Grin

@Susan, can you explain a bit more fully - I will try to work out what can be done.

NWJ

#8: 11-Mar-2007, 11:56 AM

Coding Team

sottwell
Posts: 10,530

WWW
I was able to create a "new document" template that included TVs, and when creating a new document you could specify the value for the TV. I took the code more-or-less from the Manager mutate_content file. But when attempting to get the TV's value for a given page when using it to edit an existing document, I was never able to get it to display the TV's value for that document so it could be edited.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#9: 12-Mar-2007, 06:49 PM


ChuckTrukk
Posts: 851

WWW
What type of bounty would someone need to be able to edit TV's from the frontend and make it free OS GPL'ed? I have been meaning to see what how financially feasable it would be to do this. Anyone want to throw out a number to me (PM or here).

Chuck
Chuck the Trukk
ProWebscape.com :: Nashville-WebDesign.com
- - - - - - - -
What are TV's? Here's some info below.
http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008

#10: 12-Mar-2007, 10:38 PM

Foundation

rthrash
Posts: 11,353

WWW
Chuck, that will be "just doable" with 0.9.7, beta soon after 0.9.6. Smiley
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#11: 19-Mar-2007, 02:15 PM


ChuckTrukk
Posts: 851

WWW
Ryan,

Thanks for the wonderful answer. If you already know, how will this frontend editing work (such as updated NewsEditor, new MODx api or built-in functionality). I know the manager and frontend will be almost one-and-the-same. Is there a description already of this process or functionality?

Thanks again,
Chuck
Chuck the Trukk
ProWebscape.com :: Nashville-WebDesign.com
- - - - - - - -
What are TV's? Here's some info below.
http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008

#12: 22-Mar-2007, 08:20 PM

Foundation

rthrash
Posts: 11,353

WWW
No description yet... but lots more on it in general soon. Then all the questions can be asked (over again I know, but gotta start for real at some point) and answered. Smiley
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#13: 22-Mar-2007, 09:37 PM

Moderator

OpenGeek
MODx Co-Founder
Posts: 6,977

damn accurate caricatures...

WWW
If you already know, how will this frontend editing work (such as updated NewsEditor, new MODx api or built-in functionality). I know the manager and frontend will be almost one-and-the-same. Is there a description already of this process or functionality?

It will likely be via php scripts that we'll call AJAX "connectors", or directly via the xPDO-powered model (or set of related API's) developed as MODx 0.9.7.

The connectors would be available from an isolated "connector" context, outside of the typical "web" and "mgr" contexts that we are used to in the current MODx.  It will handle validation and processing of any authorized content management related requests, regardless of the source.  But, the key is that the MODx model will provide centralized access to the domain data and logic that even these connectors will utilize, so there will be fewer limitations imposed by the poor-man's web services architecture the connectors will represent.

More on this and some related MODx 0.9.7 manager changes that I'm quite excited about, very soon.
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#14: 23-Mar-2007, 02:43 AM


ChuckTrukk
Posts: 851

WWW
Tht sounds very exciting. Can't wait to be as excited as you are about it Jason (That was serious).

Chuck
Chuck the Trukk
ProWebscape.com :: Nashville-WebDesign.com
- - - - - - - -
What are TV's? Here's some info below.
http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008

#15: 21-Aug-2008, 11:13 AM

GuillerOnLine
Posts: 26

There are any changes to continue this tutorial from Alpha 3 Revolution?

#16: 21-Aug-2008, 11:19 AM

Foundation

splittingred
Posts: 1,520

i am alt-country rock

WWW
There are any changes to continue this tutorial from Alpha 3 Revolution?

shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs

#17: 21-Aug-2008, 11:27 AM

GuillerOnLine
Posts: 26

Thanks splittingred, Nor had found examples

/om/sample/person.class.php
/om/sample/xpdosample.class.php

Neither downloading xPDO 1.0b

http://xpdo.org/api/xpdo-om-sample/_om---sample---xpdosample.class.php.html

(http://xpdo.org is not properly paginated)

I will continue these Link

#18: 21-Aug-2008, 11:45 AM

GuillerOnLine
Posts: 26

If someone else needs to translate those linked notes that neither Google nor Yahoo can.
One solution is to use
http://worldlingo.com/en/websites/url_translator.html

#19: 21-Aug-2008, 12:08 PM

Moderator

OpenGeek
MODx Co-Founder
Posts: 6,977

damn accurate caricatures...

WWW
My apologies; with my focus on MODx Revolution, xpdo.org has not received much attention from me lately.  I recommend getting the latest xpdo directly from SVN at http://svn.xpdo.org/svn/xpdo/trunk/ as there have been many improvements and changes since revision 75.  We are currently at revision 215.  I'll try and get a new package together for download this week, as well as update the documentation.
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#20: 21-Aug-2008, 12:28 PM

GuillerOnLine
Posts: 26

OpenGeek:
I shall be attentive, but insist that the next revolution is going to be editing graphics.

Quote
The ultimate goal is to offer a visual editor for designing/managing a custom Model or even altering the core MODx model, within the MODx manager itself.
http://modxcms.com/transforming-modx-oo-mvc-orm.html
   
Language through, I took a lot understand their vision, and now I'm anxious to concrete, many OpenGeek thank you very much.
Pages: [1] 2  All   Go Up
0 Members and 1 Guest are viewing this topic.