Get the parent

on 07-May-2010 | Comments ( 7 ) 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: 5
Comment
Re: Get the parent
Reply #7 on : Sat May 08, 2010, 03: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 #6 on : Sat May 08, 2010, 07: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: 5
Comment
Re: Get the parent
Reply #5 on : Sat May 08, 2010, 11: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: 5
Comment
Re: Get the parent
Reply #4 on : Sat May 29, 2010, 20: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: 5
Comment
Re: Get the parent
Reply #3 on : Sun May 30, 2010, 05:24:45
@Anonymous see this thread: http://modxcms.com/forums/index.php/topic,49364.msg288132.html#msg288132
yomismo
Posts: 5
Comment
parent...
Reply #2 on : Fri July 09, 2010, 05:46:05
you forgot [ * parent * ]
cipa
Posts: 2
Comment
Re: Get the parent
Reply #1 on : Fri July 09, 2010, 07:48:35
So 12 ways ;)
Unfortunately you can't use [ * parent * ] in your custom snippets.
Thanks

Quick modx Evolution Tags

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

Quick modx Revolution Tags

  • cached [[snippet]] or uncached [[!snippet]]
  • cached [[$chunk]] or uncached [[!$chunk]]
  • [[+placeholder]]
  • cached [[*resourceField/TV]] or uncached [[!*resourceField/TV]]
  • no timing tag - must investigate
  • [[~link]]
  • [[++systemSetting]]
  • [[%languageStringKey]]
© modxRULES! 2009-2010