1

I´m trying to change a PHP script for me project in Contao with the extension Metamodels. This is the source script that should work:

I changed the code like this.

But I get an Fatal PHP error, did i Missed something? I ´m getting into PHP right now but I´m not so far.

THX

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
Steve
  • 31
  • 5

1 Answers1

1

You missed some quotes in here:

echo "<br>======================  ".$arrItem[text][name]."<br>";

Change to:

echo "<br>======================  ".$arrItem['text']['name']."<br>";

Here's what code should look like with the correct namespaces:

// echo '<pre>' . $this->showTemplateVars() . '</pre>'; // Zeigt alle Werte aus dem MM an
// ### Tabelle mm_kalender auslesen ###
foreach ($this->data as $arrItem) {
    // ============================================
    // ### Detaildaten aus Fremdtabelle STANDORTE holen ###
    // http://de.contaowiki.org/MetaModels_Beispiel:_Referenced_items

    // Tabelle, in der die gesuchten Informationen stehen
    $objMetaModel = \MetaModels\Factory::byTableName('mm_loesungen');
    // Filterobjekt erzeugen
    $objFilter = $objMetaModel->getEmptyFilter();

    // Einen bestehenden Filter zuordnen
    // $objFilter = $objMetaModel->prepareFilter(10, array());  // 10 = Id des Filters

    // Hier werden in der MM mm_standorte alle Datensätze gesucht, die im Feld 'standort' den
    // Wert $arrItem['raw']['mitarbeiter']['standort'] haben
    $objFilter->addFilterRule(new \MetaModels\Filter\Rules\SearchAttribute($objMetaModel->getAttribute('partner'), $arrItem['raw']['name']['partner'], $objMetaModel->getAvailableLanguages()));

    // Array mit allen gefundenen Datensäten
    $objItems = $objMetaModel->findByFilter($objFilter);

    // ## KONTROLLE ##

    // Ausgabe der gefundenen Datensätze
    foreach ($objItems as $objItem) {
        echo "<br>======================  ".$arrItem['text']['name']."<br>";
        if (!empty($objItem)) {
            $arrName = $objItem->get('name');
            $arrBeschr = $objItem->get('beschreibung');
            $arrAlias = $objItem->get('alias');
        } else {
            $arrName = "???";
            $arrBeschr = "???";
            $arrAlias = "???";
        }
        echo "Name: ".$arrName['name']." | Beschreibung: ".$arrBeschr['beschreibung']." | Alias: ".$arrAlias['alias']."<br>";
    }
}
PiranhaGeorge
  • 999
  • 7
  • 13
  • Thanks but I still get the error: PHP Fatal error: Class 'MetaModelFactory' not found in /wwwroot.wwwnew/templates/metamodel_partnerpool.html5 on line 10 – Steve Jun 29 '15 at 12:36
  • It appears naming conventions have changed. See https://github.com/MetaModels/core/blob/contao3/src/system/modules/metamodels/MetaModels/Factory.php – PiranhaGeorge Jun 29 '15 at 12:43
  • That the code your using is old, and the classes it references have since changed name/namespace. For example `MetaModelFactory` is now `\MetaModels\Factory`. – PiranhaGeorge Jun 29 '15 at 12:57
  • Ok, I will try to change the class name of my template. – Steve Jun 29 '15 at 12:58
  • I changed "MetaModelFactory" to "Factory" but its still not working. – Steve Jun 29 '15 at 13:05
  • Change it to `\MetaModels\Factory` – PiranhaGeorge Jun 29 '15 at 13:05
  • Like this: $objMetaModel = \MetaModels\Factory::byTableName('mm_loesungen');? – Steve Jun 29 '15 at 13:07
  • Yup, that should do it. – PiranhaGeorge Jun 29 '15 at 13:07
  • Now I get this error: PHP Fatal error: Call to a member function getEmptyFilter() on null in /wwwroot.wwwnew/templates/metamodel_partnerpool.html5 on line 12 How can I leave the filter for now empty? Because I donwt have one right now. – Steve Jun 29 '15 at 13:09
  • Your getting this error because `$objMetaModel = \MetaModels\Factory::byTableName('mm_loesungen');` returned `null`. Looking at the `byTableName()` method, in the link I posted earlier, it's because the query returned nothing. Make sure the table exists and that it's not empty. – PiranhaGeorge Jun 29 '15 at 13:14
  • I had to replace an i with an l. -.- Oh boy now the class "MetaModelFilterRuleSearchAttribute" is missing, how can I figure out, what that class is called now? – Steve Jun 29 '15 at 13:20
  • It's this one https://github.com/MetaModels/core/blob/contao3/src/system/modules/metamodels/MetaModels/Filter/Rules/SearchAttribute.php – PiranhaGeorge Jun 29 '15 at 13:22
  • Just start at https://github.com/MetaModels/core/tree/contao3/src/system/modules/metamodels/MetaModels and follow the directory structure. It's fairly self explanatory. – PiranhaGeorge Jun 29 '15 at 13:23
  • You'll see the namespace at the top of each file. – PiranhaGeorge Jun 29 '15 at 13:24
  • Ok, so I just need to replace it with this name: "MetaModels\Filter\Rules" – Steve Jun 29 '15 at 13:27
  • Nope, `\MetaModels\Filter\Rules\SearchAttribute`. – PiranhaGeorge Jun 29 '15 at 13:28
  • Ah ok, almost, but it doesnt stop, I just put the log in a fiddle: http://phpfiddle.io/fiddle/1754380468 – Steve Jun 29 '15 at 13:30
  • I would guess that the `'partner'` attribute doesn't exist. – PiranhaGeorge Jun 29 '15 at 13:34
  • Thanks man, you helped me a lot but I#ve read the tutorial again and I'm kind of stucked because I#m not sure if the script is doing the right thing for me, what I want. – Steve Jun 29 '15 at 13:49
  • No problem. You might be better off reading some beginner PHP tutorials first. – PiranhaGeorge Jun 29 '15 at 13:55
  • Ok, here is some description of my project. I've got 2 metamodels "mm_partnerpool" and "mm_loesung". "mm_loesung" got an attribute "solution_provider" which is a select and links to the "name" of "mm_partnerpool". Because of that select I dont need a select at "mm_partnerpool" that links to "mm_loesung", I dont want to do the same work twise. The template is for the "mm_partnerpool" table e.g.: If I show the entry "Partner 1", I want to show all the Solutions("mm_loesung") that are relevant to "Partner 1". – Steve Jun 30 '15 at 08:11