df_get_record issue

132 views
Skip to first unread message

Alex Manley

unread,
Jul 22, 2016, 4:37:25 PM7/22/16
to Xataface
Hey all,

I'm trying to keep this short and sweet, basically I'm trying to make an update using an import filter.

When I call
 $exOrder = df_get_record('orders',array('ID'=7));
and print_r the $exOrder it returns the record object for ID 1. I am expecting no records because i know the record for ID 7 doesnt exist yet. Is there another call I can use that will return null when no matching records are found?

TIA,
Alex

Steve Hannah

unread,
Jul 22, 2016, 5:24:41 PM7/22/16
to xata...@googlegroups.com
There is a typo in that code:

Should be
$exOrder = df_get_record('orders',array('ID'=> 7));

But it should still not return for ID=1.  Are you sure the column is ID and not id or Id or something like that?


--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at https://groups.google.com/group/xataface.
To view this discussion on the web visit https://groups.google.com/d/msgid/xataface/33128fbf-a44d-4dc0-ad0e-a9793fcb7e8c%40googlegroups.com.



--
Steve Hannah
Web Lite Solutions Corp.

Alex Manley

unread,
Jul 25, 2016, 8:01:54 AM7/25/16
to Xataface
Absolutely positive, I typoed in my post but not in my code. I've double checked that the ID I'm returning is the correct field. It's just populating with record with ID 1 even if the record for ID 7 does not exist. I would expect it to return an error or empty array.

Alex Manley

unread,
Jul 25, 2016, 8:19:26 AM7/25/16
to Xataface
function __import__Update_Customer_orders(&$data, $defaultValues=array()){
        $records
= array();

        $rows
= explode("\n",$data);

       
foreach($rows as $row){
               
if(empty($row)){
                       
return $records;
               
}else{
                        list
($cust,$product) = str_getcsv($row);

                       
if(is_null($cust)){
                               
continue;
                       
}

                        $exCustRec
= df_get_record('Customer',array('QB_ID'=>$cust));
                        $ID
= $exCustRec->val('ID');
                       
if(empty($ID)){
                               
continue;
                       
}else{
                                $exOrdRec
= df_get_record('Details',array('ID'=>$ID));

                               
if(empty($exOrdRec)){
                       
print "new Record";exit;
                                        $newOrdRec
= new Dataface_record('Details',array());

                                        $newOrdRec
->setValues(array(
                                               
'Customer_ID'=>$ID,
                                               
'orderDateTime'=>"0000-00-00 00:00:00",                      
                                               
'wantsStatic'=>"0",
                                               
'addEth'=>"0",
                                               
'bDay'=>"LST",
                                               
'fee'=>"FREE",
                                               
'product'=>$product,
                                               
'ordStatus'=>"BILL",
                                               
'ordLocation'=>"BSM"
                                       
));

                                        $records
[] = $newOrdRec;
                               
}else{
                       
print "existing Record";exit;
                                        $exOrdRec
->setValues(array(
                                               
'product'=>$product
                                       
));

                                        $exOrdRec
->save();

                                        $records
[] = $exOrdRec;
                               
}
                       
}
               
}
       
}
       
return $records;
}

EXAMPLE DATA
: 7,FST

I've included the code for the import/update filter. I also included the example data. whenever I run the import it enters the existing record loop, which it shouldnt since id  7 deos not  exist in this table yet.

On Friday, July 22, 2016 at 5:24:41 PM UTC-4, Steve Hannah wrote:

Alex Manley

unread,
Jul 25, 2016, 1:16:30 PM7/25/16
to Xataface
Got it fixed, for future people looking to do this. Replace the df_get_record line in my above code and replace it with below and run the if on $exists. What I found was that if you try to do df_get_record on something you expect to not exist it seems to give you the lowest/first result in the table. Even runnning it as a query didn't work as it was returning the same result, however adding the $exists line seems to have cleared my issues.

$exOrdRec = mysqli_query($link,"SELECT D.Customer_ID FROM Details D WHERE D.Customer_ID = $ID");
$exists = (bool) mysqli_num_rows($exOrdRec);

Steve Hannah

unread,
Jul 25, 2016, 1:41:36 PM7/25/16
to xata...@googlegroups.com
The column names you posted in your solution don't match any of the failing examples you provided in your questions.  That makes it hard to follow.  I use df_get_record() all the time for testing existence and it works fine.  

Alex Manley

unread,
Jul 27, 2016, 1:27:03 PM7/27/16
to Xataface
I apologize for the confusion. The initial post should have been
$exOrdRec = df_get_record('Details',array('ID'=>$ID));

I thought simplifying the example would be more helpful than it was.

Steve Hannah

unread,
Jul 28, 2016, 11:27:20 AM7/28/16
to xata...@googlegroups.com
What was the value of the $ID variable there.  If it was empty, it would indeed return the first matching record.  You can ensure exact match by prepending '=' to it.

e.g.

df_get_record('Details', array('ID' => '='.$ID));

Steve

Reply all
Reply to author
Forward
Message has been deleted
0 new messages