Hey guys,
I'm advancing slowly but surely in using xPDO and yes, it's fun! From time to time I run into a wall, though, just as this one:
I wrote my first "production" PHP function yesterday, and today I'm trying the second one. As it seems, there's something spooky about using getObject() inside a function.
In the code below, there are 4 lines commented out. If I comment in the first one, the page still displays and nothing much changes, as expected. But if I comment in any of the others, something crashes and the page doesn't display anymore. Note that where I use '1' as criterion in the queries, I would put an $id, but it doesn't even work with a value hardcoded to denote a definitely existing object.
<?php
$modx->runSnippet('getInventarConnection');
$db = $modx->getPlaceholder('inventar.db');
//$cd = $db->getObject('CDs',1);
function tabellenZeile($zellen) {
$zeilenanfang = "<tr>\n <td>";
$zelltrenner = "</td>\n <td>";
$zeilenende = "</td>\n</tr>\n";
$zeile = $zeilenanfang;
foreach ($zellen as $variable => $wert) {
$zeile .= $wert;
if ($wert != end($zellen)) {
$zeile .= $zelltrenner;
}
}
$zeile .= $zeilenende;
return $zeile;
}
function gibInhalte($rechnungsobjekt) {
$inhalte = $rechnungsobjekt->getMany('Rechnungsinhalte');
$rechnungsinhalt = "";
foreach ($inhalte as $inhalt) {
$typ = "";
$typ = $inhalt->get('Inhaltstyp');
$id = $inhalt->get('InhaltsID');
switch ($typ) {
case 1:
$rechnungsinhalt .= "CD ";
//$cd = $db->getObject('CDs',1);
if (!empty($cd)) : $name = $cd->get('Titel'); else: $name = ""; endif;
break;
case 2:
$rechnungsinhalt .= "Softwarebox ";
//$softwarebox = $db->getObject('Softwareboxen',1);
if (!empty($softwarebox)) : $name = $softwarebox->get('Software'); else: $name = ""; endif;
break;
case 3:
$rechnungsinhalt .= "Lizenz ";
//$lizenz = $db->getObject('Lizenzen',1);
if (!empty($lizenz)) : $name = $lizenz->get('Softwaretitel'); else: $name = ""; endif;
break;
}
$rechnungsinhalt .= $id.": ".$name;
$rechnungsinhalt .= "
\n";
}
return $rechnungsinhalt;
}
...
Oh, I should add that even where my code works, when the $id can't be found, the getObject() calls do not return null as advertised, but rather cause php to crash or in any case no longer produce page output. Maybe that's something to do with my main issue?
Thanks a bunch for having a look!
René
[ed. note: neo2.0 last edited this post 14 years, 2 months ago.]