This topic is locked
[SOLVED]

 $DAL equivalent of INSERT xxx ON DUPLICATE KEY UPDATE yyy

9/26/2014 6:32:33 AM
PHPRunner General questions
J
JER author

I'm trying to go over to using using DAL structures as a way to avoid concatenating long text strings (with tricky detail/checking around " and ' etc) to make SQL statements...
Is there any assumption built into the way that the add() function operates that would assist in building a simpler/more structured DAL equivalent?
In an add() the inputs are all VALUEs while for update() key inputs are PARAMs and non keys are VALUEs still.
In the older DAL syntax the declaration of VALUE vs PARAM was not explicit so I suppose I'm hoping that there is some decision logic behind the DAL structure that detects key field and which may not be documented thus far?

Sergey Kornilov admin 9/26/2014

What exactly is the question? What kind of issue you dealing with?

J
JER author 9/26/2014

I am trying to code a simple task to either update a record if it exists or insert a new record in a table which has two key fields.
I would normally prepare a long SQL command like:

" INSERT INTO table ( Field_A, Field_B, Field_C) VALUES ( '" . $variable_a ."' ,'" . $variable_b . "' , '". $variable_c . "') {wrap}

ON DUPLICATE KEY UPDATE Field_C = '" . $variable_c . "' "
(Key fields are 'A' and 'B')
This is a right pain to make sure all the various " and ' are exact.
I was wondering (hoping) if the DAL layer had an inbuilt method of add() on DUPLICATE KEY update().

It might look something like:
global $dal;

$tblEvents = $dal->Table("EventsTable");

$tblEvents->Param["event"]="First event";

$tblEvents->Param["public"]="yes";

$tblEvents->Value["cost"]="$10";

$rs = $tblEvents->Add();
if $rs = duplicate

$tblEvents->update();
Where event and public are keys

Also assuming Add() defaults to using Param as Value
I can use a ->FetchbyID before either add() or update() but then on a NULL record result I have to redefine all the Params to Values which is repetative.

I far prefer the DAL method/syntax but it seems difficult to code an equivalent of INSERT xxx ON DUPLICATE KEY UPDATE yyy to take advantage of it.
Thanks

Sergey Kornilov admin 9/26/2014

I see what you mean. No, DAL's Add() method doesn't support ON DUPLICATE KEY UPDATE.

You either need to build SQL query manually or do select first and then either Add or Update via DAL.