Get the parent

on 07-May-2010 | Comments ( 8 ) Tags: Evolution

Here are 11 examples on how you can get the parent of the current document in MODx.

Using a snippet

UltimateParent

<?php
/*UltimateParent*/
$parent = $modx->runSnippet("UltimateParent");
echo  'UltimateParent: ' . $parent;
?>

GetField

<?php
/*GetField*/
$parent = $modx->runSnippet("GetField", array('field' => 'id', 'parent' => '1'));
echo  'GetField: ' . $parent;
?>

ParentParent

<?php
/*ParentParent*/
$parent = $modx->runSnippet("ParentParent");
echo  'ParentParent: ' . $parent;
?>

Using MODx API

getParent

<?php
/*getParent*/
$parent = $modx->getParent($modx->documentIdentifier,'','id');
echo  'getParent: ' . $parent['id'];
?>

getParentIds

<?php
/*getParentIds*/
$parent = $modx->getParentIds($modx->documentIdentifier);
echo  'getParentIds: ' . reset($parent);
?>

getDocument

<?php
/*getDocument*/
$parent = $modx->getDocument($modx->documentIdentifier, 'parent');
echo  'getDocument: ' . $parent['parent'];
?>

getPageInfo

<?php
/*getPageInfo*/
$parent = $modx->getPageInfo($modx->documentIdentifier, '', 'parent'); 
echo  'getPageInfo: ' . $parent['parent'];
?>

Using MODx DBAPI

db->select + db->getValue

<?php
/*db->select + db->getValue*/
$parent = $modx->db->getValue($modx->db->select("parent", $modx->getFullTableName('site_content'), "id=" . $modx->documentIdentifier));
echo  'db->select + db->getValue: ' . $parent;
?>

db->select + db->getRow

<?php
/*db->select + db->getRow*/
$parent = $modx->db->getRow($modx->db->select("parent", $modx->getFullTableName('site_content'), "id=" . $modx->documentIdentifier));
echo  'db->select + db->getRow: ' . $parent['parent'];
?>

db->query + db->getValue

<?php
/*db->query + db->getValue*/
$parent = $modx->db->getValue($modx->db->query( 'SELECT parent FROM ' . $modx->getFullTableName('site_content') .' WHERE id =' . $modx->documentIdentifier ));
echo  'db->select + db->getValue: ' . $parent;
?>

db->query + db->getRow

<?php
/*db->query + db->getRow*/
$parent = $modx->db->getRow($modx->db->query( 'SELECT parent FROM ' . $modx->getFullTableName('site_content') .' WHERE id =' . $modx->documentIdentifier ));
echo  'db->query + db->getRow: ' . $parent['parent'];
?>

Leave a message if you know other ways to do get the parent Id.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
sharkbait
Posts: 6
Comment
Re: Get the parent
Reply #8 on : Sat May 08, 2010, 02:21:06
haha, wow .. that's a lot :) / wouldn't it be better to use "return" instead of "echo"?
thanks, j
cipa
Posts: 2
Comment
Re: Get the parent
Reply #7 on : Sat May 08, 2010, 06:55:40
Hi sharkbait,

I couldn't find a reason why return is better than echo. The code is executed using eval and I don't think eval cares about it.

Can you please post an example of why return is better? Thanks
sharkbait
Posts: 6
Comment
Re: Get the parent
Reply #6 on : Sat May 08, 2010, 10:03:16
Hi cipa,

couldn't find a very detailed post, but i've read many times that it's best practice. Echoing works too, but the results might be unpredictable sometimes

http://modxcms.com/forums/index.php/topic,43701.msg260712.html#msg260712

http://modxcms.com/forums/index.php/topic,48057.msg281745.html#msg281745

http://modxcms.com/forums/index.php/topic,29404.msg178745.html#msg178745

thanks, j
Anonymous
Posts: 6
Comment
Re: Get the parent
Reply #5 on : Sat May 29, 2010, 19:05:07
Unfortunately return doesn't always work when you call a snippet inside another snippet. I remember having a problem with the includeTemplate snippet I use to keep my templates in files.
sharkbait
Posts: 6
Comment
Re: Get the parent
Reply #4 on : Sun May 30, 2010, 04:24:45
@Anonymous see this thread: http://modxcms.com/forums/index.php/topic,49364.msg288132.html#msg288132
yomismo
Posts: 6
Comment
parent...
Reply #3 on : Fri July 09, 2010, 04:46:05
you forgot [ * parent * ]
cipa
Posts: 2
Comment
Re: Get the parent
Reply #2 on : Fri July 09, 2010, 06:48:35
So 12 ways ;)
Unfortunately you can't use [ * parent * ] in your custom snippets.
Thanks
Viktorminator
Posts: 6
Comment
Re: Get the parent
Reply #1 on : Wed January 18, 2012, 02:33:56
Even if it gets parent ID in any snippet, you`d better mention that get parent ID in MODx, because I didn`t know about this tag before today and thought "Why doesn`t MODx have some simple tag for parent?" =)

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014