Jul 05, 2009, 08:40 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Search via SMF or Google: modx forums all of modxcms.com web
  MODxCMS.com   Forums   Help Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Introducing xPDOQuery  (Read 6234 times)
0 Members and 1 Guest are viewing this topic.
OpenGeek
MODx Co-Founder
Moderator
*
Posts: 5,814


damn accurate caricatures...


WWW
« on: Feb 15, 2007, 01:57 PM »

I'm working hard to get the xPDO documentation up to date and more complete.  In the meantime, I wanted to introduce everyone to the latest feature additions to xPDO 1.0 alpha (currently only available from SVN; I'll update the download packages later today) as of revision 27.

xPDOQuery -- Ok, so I decided some SQL abstraction was necessary.  Tongue

Based on feedback from a few early adopters of xPDO, I have added a significant new extension to the xPDO arsenal.  Previously, in order to do more complex queries on the objects, it was necessary to write the SQL to do so.  You could wrap it with an xPDOCriteria object to pass it into the xPDO object handling methods along with some bindings.  But all too common are slight variations of the scaffolding SQL that is automatically handled by the object layer, and we needed a way to get record counts, apply limits, order by clauses, additional where conditions, etc.  Enter xPDOQuery.

xPDOQuery extends the xPDOCriteria class, allowing programmatic control over the generated SQL, via a logical API.  You can still manually wrap SQL with xPDOCriteria, but compare the two approaches:

Old way, using xPDOCriteria:
Code:
<?php
$webUserTable
$xpdo->getTableName('modWebUser');
$sql"SELECT * FROM {$webUserTable} WHERE `id` = :user_id LIMIT 1";
$bindings= array(
    
':user_id' => array ('value' => $userid'type' => PDO_PARAM_INT)
);
$criteria= new xPDOCriteria($this->xpdo$sql$bindings);
$user$xpdo->getObject('modWebUser'$criteria);

Same query, new way, using xPDOQuery (via $xpdo->newQuery()):
Code:
<?php
$criteria
$xpdo->newQuery('modWebUser'$userid);
$criteria->limit(1);
$user$xpdo->getObject('modWebUser'$criteria);
and in PHP 5, you can even use the shorthand...
Code:
<?php
$criteria
$xpdo->newQuery('modWebUser')->where($userid)->limit(1);
$user$xpdo->getObject('modWebUser'$criteria);
Logged

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
davidm
MODx evangelist
Marketing & Design Team
*
Posts: 7,026


Software is like sex, it's better when it's free !


WWW
« Reply #1 on: Feb 15, 2007, 05:31 PM »

From my limited perspective code-wise, I can't say I am unhappy with having some SQL abstraction and xPDOQuery seems like something I'd be very comfortable using Smiley

I sure am grateful for the update Jason, especially since I try to wrap my head around the whole paradigm shift that the new core is... this heads up will make things easier and smoother for sure...
Logged

.: nodeo.net : Pour un web libre, moderne et ouvert ! :: david-molliere.net : Suivez en "live" mes expérimentations et billets sur les CMS et autres applications web :.

*** Forums modxcms.fr Participez ŕ l'élaboration du site MODx francophone ! ***

! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accčs pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

Réalisations sous MODx : | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net
kylej
Coding Team
*
Posts: 758



WWW
« Reply #2 on: Feb 15, 2007, 07:00 PM »

Jason,
   That looks great, I definatly would have used that on my fantasy site.  I think this will really add to the flexibility and easy of use for xPDO.  Well done.

Kyle
Logged

OpenGeek
MODx Co-Founder
Moderator
*
Posts: 5,814


damn accurate caricatures...


WWW
« Reply #3 on: Feb 15, 2007, 09:56 PM »

Also, just to exemplify how xPDOQuery helps to really expand the power of the object layer, here are some new functions for xPDO itself which came about as a result of the new xPDOQuery capabilities:
NOTE: A criteria referred to in any of the xPDO parameters below, can be an xPDOCriteria instance, an xPDOQuery instance, a single string or integer primary key value, an array of primary key values in the proper order, or an xPDO conditional expression.  See the examples below:

getCount(className, criteria): returns a count of records of a particular class, optionally using a specified criteria.
Code:
<?php 
// counts all web user records
$xpdo->getCount('modWebUser'); 
// counts all web user records with a username that starts with the letter a
$xpdo->getCount('modWebUser'$xpdo->newQuery('modWebUser', array ('username:LIKE' => 'a%'));

getObjectGraph(className, graph, criteria): returns an object with related objects populated in a single query; automated joins specified by a nested array (or JSON representation of an array) of related classes and their relation keys
Code:
<?php 
// gets a web user with an id of 1, along with their related user profile record
$xpdo->getObjectGraph('modWebUser''{modWebUserProfile:{internalKey:{}}}'1);

getCollectionGraph(className, graph, criteria): returns a collection of objects with related objects populated in a single query
Code:
<?php 
// gets all web users and their profiles, sorted by username
$xpdo->getCollectionGraph('modWebUser''{modWebUserProfile:{internalKey:{}}}'$xpdo->newQuery('modWebUser')->sortby('modWebUser.username'));
« Last Edit: Feb 15, 2007, 09:58 PM by OpenGeek » Logged

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
PaulGregory
MODx's midnight runner
Committed to MODx
*****
Posts: 1,097

MODx's midnight runner


WWW
« Reply #4 on: Feb 16, 2007, 05:43 AM »

getCount, getObjectGraph and getCollectionGraph sound great.

Returning to your first example, I can't see how xPDOQuery knows that $userid should be compared with `id`.
Is that the default, or is that configured somewhere, or is $userid compared against all fields, or what?

And how would you compare $userid against another field instead like `introducedby`?  Would that use the array method seen in the more recent examples?

Logged

No, I don't know what OpenGeek's saying half the time either.
MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
Forum: Where to post threads about add-ons | Forum Rules
Like MODx? donate (and/or share your resources)
Like me? See my Amazon wishlist
MODx "Most Promising CMS" - so appropriate!
kylej
Coding Team
*
Posts: 758



WWW
« Reply #5 on: Feb 16, 2007, 07:13 AM »

Paul,  I think in Jasons first example userid id being compared to the primary key, and if you would want to use it against a different field the array method would work.
Logged

OpenGeek
MODx Co-Founder
Moderator
*
Posts: 5,814


damn accurate caricatures...


WWW
« Reply #6 on: Feb 16, 2007, 11:42 AM »

Right, basically a criteria can be any of the following:

  • a primary key value
  • an array of primary key values in the correct column order (for compound primary keys)
  • an array expression in the form array( '[tableAlias.]fieldName[:operator]' => value)
  • an xPDOCriteria instance (or xPDOQuery, since it is a derivative class of xPDOCriteria)

xPDO knows what the primary key fields are and if you just pass a scalar or an array of scalars without associative keys, xPDO assumes that's what you are searching by.
Logged

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
pixelchutes
Coding Team
*
Posts: 839



WWW
« Reply #7 on: Feb 16, 2007, 12:11 PM »

Awesome stuff, Jason. I love it!
Logged

Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.
airoctive
Jr. Member
*
Posts: 14


« Reply #8 on: Jul 12, 2008, 02:55 PM »

Is it possible to build a WHERE field_name IN clause using this class?
Logged
OpenGeek
MODx Co-Founder
Moderator
*
Posts: 5,814


damn accurate caricatures...


WWW
« Reply #9 on: Jul 13, 2008, 01:27 PM »

Is it possible to build a WHERE field_name IN clause using this class?
Sure; here is an example where the criteria expression key is used with a modifier indicating to use the IN operator, rather than the default operator (=):
Code:
$events= $xpdo->getCollection('Events', array ('id:IN' => '(2080,2081,2082,2083,2084,2085)'));
or for some strings:
Code:
$events= $xpdo->getCollection('Events', array ('name:IN' => '("EventA","EventB","EventC")'));
Logged

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
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Copyright © 2005-2008 MODxCMS, All rights reserved. Contact Us
Styles by ziworks.com

Powered by SMF | SMF © 2006-2008, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!