We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36565
    • 73 Posts
    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.]
      PHP illiterate MODx enthusiast
      • 4172
      • 5,888 Posts
      I think, your function gibInhalte doesn't know anything about the existence of your $db - object
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 36565
        • 73 Posts
        Hm, too bad!
        How would I tell gibInhalte that $db exists?

        Thanks!
          PHP illiterate MODx enthusiast
          • 4172
          • 5,888 Posts
          first: I wouldn't put the db-object into a placeholder, but put it to the modx-object

          $modx->inventar_db = & $yourdbobject;


          then you can do in your functions:

          function gibInhalte($rechnungsobjekt) {
              global $modx;
              $db = & $modx->inventar_db;
          }
          
          


          I would also create a new class with your functions and put a modx-reference to your class - vars
          then you have access in all your functions in your class to the modx-object with

          $this->modx;



            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 36565
            • 73 Posts
            Thanks, Bruno, you're awesome!

            I don't yet completely understand what's happening here, but it definitely works! I have replaced $db = $modx->getPlaceholder... in all my snippets with $db = $modx->inventar_db, and everything is as before PLUS the getObjects calls inside of functions work! Hooray!


            • Could you explain what does '= &' mean other than '='?
            • And what does "put a modx-reference to your class - var" mean?
            Regarding your class proposal, I'm excited to implement my first class soon, but I don't feel quite ready. There is one other problem that I'd like to have sorted out beforehand, and I'm opening another thread for it.

            Thanks again! [ed. note: neo2.0 last edited this post 14 years, 2 months ago.]
              PHP illiterate MODx enthusiast
              • 4172
              • 5,888 Posts
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 36565
                • 73 Posts
                [reading]
                  PHP illiterate MODx enthusiast