import fails

12 views
Skip to first unread message

tom wible

unread,
Sep 11, 2025, 5:44:29 PMSep 11
to Xataface
i've created the import hook:

    function __import__csv(&$data, $defaultValues=array())
    {
    $rv = cghoa_import_csv($data, $defaultValues,
        'owners',
      array("ownerID","firstNAME","LASTNAME","role","email","eDeliveryConsent","phone"));
      if ( is_string($rv) ) // errors
      {
          print_r($rv);
          exit;
      }
      return $rv;
    }

with the utility function:

    function cghoa_import_csv(&$data, $defaultValues,
                              $tableName,
                              $reqdFields = array(),
                              $RS = "\n", // not the same as '\n' wtf?
                              $FS = ',' )
    {
        $skipit      = "SKIP";
        $rows        = explode($RS, $data);
        $row         = array_map('trim', explode($FS, array_shift($rows)));

        $errs        = array();
        $inputFields = array();
        foreach ( $row as $fieldName )
        {
            if (($fieldName == $skipit )
             or (array_key_exists($fieldName, $defaultValues)))
            {
                $inputFields[] = $fieldName;
            }
            else
            {
                $inputFields[] = $skipit; // keep count aligned
                $errs[] = print_r("******ERROR: invalid field name: ".$fieldName."*****", TRUE);
            }
        }
        $nf = count($inputFields);

        foreach ($defaultValues as $fieldName => $defVal)
        {
            if ( in_array($fieldName, $reqdFields)
            and !in_array($fieldName, $inputFields)
            and  empty($defVal) )
                $errs[] = print_r("******ERROR: required field missing: ".$fieldName."*****", TRUE);
        }

        $records = array();
        $nr      = 0;
        foreach ( $rows as $row )
        {
            $values = explode($FS, $row);
            $nr++;
            $cv     = count($values);
           
            if ($cv != $nf)
            {
                $errs[] = print_r("******ERROR: expecting ".$nf." fields but found ".$cv." in record# ".$nr.": ".$row, TRUE);
                continue;
            }

            $record = new Dataface_Record($tableName, array());
            $record->setValues($defaultValues);
            for($i = 0;$i<$nf;$i++)
                if ($inputFields[$i] != $skipit )
                    $record->setValue( $inputFields[$i], trim($values[$i]) );
            $records[] = $record;
        }

        if (! empty($errs))
            return implode("<br>", $errs);

        return $records;
    }
which works fine elsewhere
entered csv:

"ownerID","firstNAME","LASTNAME","role","email","eDeliveryConsent","phone"
1 Mark & Krista Donlon BOARDMEMBER ...@aba.com 02/18/21 703...
2 Badiollah Shahidian HOMEOWNER 2...@cg.org 703...
3 James & Margaret Kirkwood HOMEOWNER 3...@cg.org 703...
4 Thomas & Carmen Torbert HOMEOWNER 4...@cg.org
5 Andrew Wyczalkowski Estate, C/O: CATHY KINNIBURGH... HOMEOWNER ...@inova.org 703...

preview shows expected values (i'm trying to replace dummy emails with valid ones):

OwnerID FirstNAME LASTNAME Role Password Email Consent to Electronic Communications Phone
1 Mark & Krista Donlon BOARDMEMBER kdonlon@... 02/18/21 703...
2 Badiollah Shahidian HOMEOWNER Badiollah.Shahidian@... 703...
3 James & Margaret Kirkwood HOMEOWNER 3...@cg.org 703...
4 Thomas & Carmen Torbert HOMEOWNER Thomas.Carmen.Torbert@...
5 Andrew Wyczalkowski Estate C/O: CATHY KINNIBURGH EXECUTOR HOMEOWNER Cathy.kinniburgh@... 703...

looks good proceed with import, returns 
  • Records imported successfully.
but no change...is the missing password field the problem?

tom wible

unread,
Sep 11, 2025, 6:59:17 PMSep 11
to Xataface
ah, i just noticed             $record = new Dataface_Record($tableName, array());
it's been 10yrs since i wrote that...
apparently import tries to create a new record, and fails on dupe instead of updating existing...how would it do that? maybe chatgpt knows;-)

Steve Hannah

unread,
Sep 11, 2025, 8:40:33 PMSep 11
to xata...@googlegroups.com
That should be fine, as far as i recall.

Maybe try just setting the values as that 2nd argument of the constructor instead of passing an empty array.  Then you don't need to call setValues.

Steve


--
You received this message because you are subscribed to the Google Groups "Xataface" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xataface+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/xataface/62fc9bb8-d02e-4d6f-bc06-5b5198331f8en%40googlegroups.com.

tom wible

unread,
Sep 12, 2025, 10:47:23 AMSep 12
to Xataface
thanx, steve, i'm looking at what chatgpt suggested:

        // if keyfield is missing or empty, create new record, otherwise update
        $table = Dataface_Table::loadTable($tableName);
        $primaryKeyCols = $table->keys(); // Get the single primary key column
        $keyField = $primaryKeyCols[0];   // assumes only one PK column

        $create = ($keyIndex === false) or empty(trim($row[$keyIndex])); // i hope php shortcircuits;-)
         if ($create)

                $record = new Dataface_Record($tableName, array());
         else
                $record = df_get_record( $tableName,
                                        [ $keyField => trim( $row[$keyIndex] ) ] );

btw, where do i set "Import Format Specific Instructions:"?


Reply all
Reply to author
Forward
0 new messages