We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hi, I've got a question about joining 2 tables, where the fields that I want to join on are not exactly the same, but the one field contains the value from the other field.

    It's for the addon Formz. I'm working on the CSV export. For some reason, the fields of the forms that are submitted and saved to the database are in a random order. In the export, the fields have to have the order that is set in the form itself.

    The order of the fields is set in the DB table modx_formz_forms_fields. This table has a field 'settings' with values like: {"label":"Email","default":"Your email"} and a field order with a number.

    The data of the forms itself is saved in a table modx_formz_forms_data_fields and it has a field 'label' which holds the label that is also present in the field settings in the other table.

    My goal is to get the form data for one form in the order that the labels are set in modx_formz_forms_fields, and put this in the CSV file.

    I tried this:

                $c = $this->modx->newQuery('fmzFormsDataFields');            
                $c->select(array(
                    $this->modx->getSelectColumns('fmzFormsDataFields', 'fmzFormsDataFields'),
                    $this->modx->getSelectColumns('fmzFormsFields', 'fmzFormsFields')           
                ));
                $c->leftJoin('fmzFormsFields', 'fmzFormsFields', array('%fmzFormsFields.settings% LIKE fmzFormsDataFields.label'));
                
                $whereArray = array(
                   'fmzFormsDataFields.data_id' => $item['id']
                );
                $c->where($whereArray);
                $c->sortby('fmzFormsFields.order','ASC');
                
                //$c->prepare();            
                //$this->modx->log(modX::LOG_LEVEL_ERROR,'SQL: '.$c->toSQL());
                
                $fieldsData = $this->modx->getCollection('fmzFormsDataFields',$c);



    Unfortunately it's not working... the SQL looks like this:

    SELECT `fmzFormsDataFields`.`id`, `fmzFormsDataFields`.`data_id`, `fmzFormsDataFields`.`label`, `fmzFormsDataFields`.`value`, `fmzFormsFields`.`id`, `fmzFormsFields`.`form_id`, `fmzFormsFields`.`type`, `fmzFormsFields`.`settings`, `fmzFormsFields`.`order` FROM `modx_formz_forms_data_fields` AS `fmzFormsDataFields` LEFT JOIN `modx_formz_forms_fields` `fmzFormsFields` ON %fmzFormsFields.settings% LIKE fmzFormsDataFields.label WHERE `fmzFormsDataFields`.`data_id` = 63 ORDER BY fmzFormsFields.order ASC 


    And the error I'm getting is:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%fmzFormsFields.settings% LIKE fmzFormsDataFields.label WHERE `fmzFormsDataField' at line 1

    So I'm not really sure how to do a leftjoin and even not sure if it's the right join to use for this. I hope someone can help me with this!
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      hmm...
      IDK if the way you store the value (as JSON) of the settings data is a proper way, but I always place the plan-to-be-queried data into separate fields.
      If you need flexible keys of the fields, then probably you need to normalize the DB little bit more.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 19328
        • 433 Posts
        Thanks goldsky.. I'm using an existing addon (Formz) so have no influence on the DB, and besides that I'm not an expert in programming. And I just found out that my logic was flawed anyway. Because the labels in the json arrays are not unique at all so this wouldn't work.

        Thanks for responding though, I appreciate it!!