Re: Weird $_GET behaviour with DB_DataObject_FormBuilder

From: Date: Sat, 10 Jul 2004 23:47:18 +0000
Subject: Re: Weird $_GET behaviour with DB_DataObject_FormBuilder
References: 1 2 3 4 5  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-13621@lists.php.net to get a copy of this message
You are so right. I don't *understand* why these two things would be related-- ie, why does the behaviour misfire when using the $_GET value, but not misfire when using any other variable? It would seem to me that if the lack of the hidden field stopped the updates when using $_GET, it would stop the updates when using other variables as well. But you nailed it. As ever, thanks. s. Justin Patrin wrote: > Ok, I know what it is now. > > Your problem is that FormBuilder (and DataObject) don't know what > record to update when you submit the form. Try adding a hidden field > to the form with the ID in it and using that (when submitted) to do > the get(). > > On Fri, 09 Jul 2004 17:20:34 -0500, Sarah Gray <sarah@fabled.net> wrote: > > Hi -- > > > > This is bizarro -- something's amiss. I also had tried > > $do->get('program_id', $e); earlier > > > > I also tried each one of these in turn: > > $e = (int)$_GET['id']; > > $e = (string)(int)$_GET['id']; > > $e = (int)($_GET['id']); > > $e = intval($_GET['id']); > > > > Sadly, this didn't work. Or rather, it worked in the same > > way the other examples worked -- fine when using any value > > not from $_GET. > > > > Here are my experiments all using DB_DataObject::debugLevel(5). > > Thanks for that tip. > > > > 1. Sample 1: My code (All code still using the exact same sample as before) > > $e = intval($_GET['id']); > > echo 'GETTYPE=' . gettype($e); // this echoes GETTYPE=integer > > $do->get($e); > > > > DEBUG CODE SAYS: > > dataobjects_mc_programs: QUERY: SELECT * FROM mc_Programs WHERE > > mc_Programs.program_id = 3 > > > > I alter data, click submit. DEBUG CODE SAYS: > > > > dataobjects_mc_programs: QUERY: INSERT INTO mc_Programs (program_name , > > program_description , program_objectives , program_skills ) VALUES ('Phenomenal > > Great News' , 'FSfdsdfsdfs' , 'sdfsdf' , 'sdf' ) > > dataobjects_mc_programs: query: QUERY DONE IN 0.00061702728271484 seconds > > FormBuilder: ID (program_id) of the new object: 64 > > > > So it clearly inserted a new row. Weird. > > > > 2. Sample 2 > > $do->get(3) > > > > DEBUG CODE SAYS: > > dataobjects_mc_programs: QUERY: SELECT * FROM mc_Programs WHERE > > mc_Programs.program_id = 3 > > > > I alter data, click submit. DEBUG CODE SAYS: > > > > dataobjects_mc_programs: QUERY: UPDATE mc_Programs SET program_name = '545454Great > > News' , program_description = 'FSfdsdfsdfs' , program_objectives = > > '' , > > program_skills = '' WHERE mc_Programs.program_id = 0003 > > dataobjects_mc_programs: query: QUERY DONE IN 0.00068092346191406 seconds > > dataobjects_mc_programs: 1: Clearing Cache for dataobjects_mc_programs > > FormBuilder: Object updated. > > > > So it clearly updated the object. > > > > 3. Sample 3: > > $e = 3; > > $do->get($e); > > > > DEBUG CODE SAYS: > > dataobjects_mc_programs: QUERY: SELECT * FROM mc_Programs WHERE > > mc_Programs.program_id = 3 > > > > I alter data, click submit. DEBUG CODE SAYS: > > dataobjects_mc_programs: QUERY: UPDATE mc_Programs SET program_name = 'Smashing > > Great News' , program_description = 'FSfdsdfsdfs' , program_objectives = > > 'sdfsdf' > > , program_skills = 'sdf' WHERE mc_Programs.program_id = 0003 > > > > So it clearly updated the object all times but the first. > > The thing is, this must be the most basic usage of this, I'm sure you use it all > > the time this way, and other folks, too. > > So it's hard to believe this could be a bug. > > > > It's a good thing girls don't go bald so easy -- I'm definitely losing hair > > over > > this! ;) > > > > s. > > > > > > > > > > Sarah Gray wrote: > > > > > Hi Justin, > > > > > > Yes, I had tried (int)($_GET['id']) as well, just didn't mention it > > > here as my > > > email > > > was getting so damn long! ;). > > > Didn't try intval, will do. And I'll try the other stuff -- but I just > > > mention > > > that I've seen this working fine in 1.3.4 > > > which makes me confused. > > > > > > As for tying $e right -- I should hope so!-- If not, I've got more problems > > > than I know ;) > > > > > > Will try debug level and let you know. > > > > > > s. > > > > > > Justin Patrin wrote: > > > > > > > Very strange behavior.... I would try a few more things. > > > > > > > > $e = (int)$_GET['id']; > > > > > > > > This is the right way to cast to an int. You could also try: > > > > > > > > $e = (int)($_GET['id']); > > > > > > > > $e = intval($_GET['id']); > > > > > > > > Are you sure you're using $e right? Typed the var right? > > > > > > > > You could also try: > > > > > > > > $e = (string)(int)$_GET['id']; > > > > > > > > Are you sure the right key is being SELECTed? Try doing: > > > > > > > > DB_DataObject::debugLevel(5); > > > > > > > > and seeing what the SQL looks like. > > > > > > > > Try: > > > > > > > > $do->get('id', $e); > > > > > > > > On Fri, 09 Jul 2004 16:37:42 -0500, Sarah Gray <sarah@fabled.net> wrote: > > > > > Hello Pear List, > > > > > > > > > > This one is weird. Thanks in advance to anyone with insight / advice. > > > > > > > > > > Summary: > > > > > php 4.3.2 and the formBuilder in Beta (not from CVS), 0.9.0 > > > > > When I try to use a value passed in from the $_GET array to plug into > > > > > my DataObject ( for editing form objects), > > > > > DB_DataObject_FormBuilder does not properly update the object. In all > > > > > cases it *does* fill in the form values with the right > > > > > values from said object -- it just does not discrimate correctly between > > > > > insert and update. > > > > > When I use plain ints or other variables that are not from $_GET, the > > > > > thing works fine. > > > > > > > > > > Multiple tests seem to trace to: every time I try using the $_GET array > > > > > values (as values or as a conditional), my code does not work as > > > > > planned. > > > > > > > > > > Is there any reason this code block shouldn't work? To create a new > > > > > object or edit existing... > > > > > > > > > > $do = DB_DataObject::factory('mc_Programs'); > > > > > if( isset($_GET['id']) ){ > > > > > $value = $_GET['id'] + 0; // to cast the value to int > > > > > $do->get($value); > > > > > } > > > > > $fg =& DB_DataObject_FormBuilder::create($do); > > > > > > > > > > If it shouldn't work -- why not? > > > > > If it should work, it doesn't. > > > > > I've listed all my code samples / examples below and would love > > > > > advice. > > > > > > > > > > Details and extended examples below this line: > > > > > > > > > > My entire code to create new object. Works. > > > > > > > > > > $do = DB_DataObject::factory('mc_Programs'); > > > > > $fg =& DB_DataObject_FormBuilder::create($do); > > > > > $form =& $fg->getForm(); > > > > > $form->addElement('image', 'submit', > > > > > '/images/submit_CreateNewProgram.gif' ); // IMG submit > > > > > if ($form->validate()) { > > > > > $form->process(array(&$fg,'processForm'), false); > > > > > $form->freeze(); > > > > > } > > > > > > > > > > Now, same code, but to edit existing object // Works. updates object > > > > > with pk 3 > > > > > > > > > > $do = DB_DataObject::factory('mc_Programs'); > > > > > $do->get(3); > > > > > $fg =& DB_DataObject_FormBuilder::create($do); > > > > > $form =& $fg->getForm(); > > > > > $form->addElement('image', 'submit', > > > > > '/images/submit_CreateNewProgram.gif' ); // IMG submit > > > > > if ($form->validate()) { > > > > > $form->process(array(&$fg,'processForm'), false); > > > > > $form->freeze(); > > > > > } > > > > > > > > > > The following examples are working / not working in a way I don't > > > > > understand. > > > > > > > > > > NOTE: I am only writing the code that is inserted between this line: > > > > > $do = DB_DataObject::factory('mc_Programs'); > > > > > and this line: > > > > > $fg =& DB_DataObject_FormBuilder::create($do); > > > > > But I have made no changes to the rest of the code block that I printed > > > > > above. > > > > > > > > > > EXAMPLES > > > > > 1. This works: // updates object with pk 3 > > > > > $e = 3; > > > > > $do->get($e); > > > > > > > > > > 2. This works // updates object with pk 3 > > > > > $e = '3'; // assign string ( to mock how $_GET['id'] > > > > > comes in as a > > > > > string) > > > > > $e = $e + 0; // switch to int, just like I did with $_GET['id'] > > > > > $do->get($e); > > > > > > > > > > 3. This DOESN'T Work (results of echo and print_r statements printed > > > > > next to if statements so you can see results) > > > > > Instead, this creates a new entry. > > > > > > > > > > if( $_GET['id'] ){ > > > > > echo 'in if > > > > > <br>'; > > > > > // in if > > > > > echo 'gettype ID=' . gettype( $_GET['id'] ) . > > > > > '<br>'; // gettype > > > > > ID=string > > > > > print_r( $_GET > > > > > ); // Array ( [id] > > > > > => 0002 ) > > > > > $e = $_GET['id'] + 0; > > > > > echo '<br>e: ' . $e . '<br>'; > > > > > // e: 2 > > > > > echo "gettype e=" . gettype($e); > > > > > // > > > > > gettype e=integer > > > > > $do->get($e); > > > > > echo '<br>leaving > > > > > if'; // leaving if > > > > > } > > > > > > > > > > 4. And, if I do this -- leaving the $do->get OUTSIDE of the if > > > > > statement, > > > > > > > > > > if( $_GET['id'] ){ > > > > > echo 'in if > > > > > <br>'; > > > > > // in > > > > > if > > > > > echo 'gettype ID=' . gettype( $_GET['id'] ) . > > > > > '<br>'; // > > > > > gettype ID=string > > > > > print_r( $_GET > > > > > ); // Array ( > > > > > [id] => 0001 ) > > > > > $e = $_GET['id'] + 0; > > > > > // e: 1 > > > > > echo '<br>e: ' . $e . '<br>'; > > > > > echo "gettype e=" . gettype($e); > > > > > // gettype e=integer > > > > > echo '<br>leaving > > > > > if'; //leaving if > > > > > } > > > > > > > > > > $do->get($e); > > > > > > > > > > I get this error: > > > > > DB_DataObject Error: No Value specified for getdb_dataobject_error > > > > > Object > > > > > ( > > > > > [error_message_prefix] => > > > > > [mode] => 16 > > > > > [level] => 1024 > > > > > [code] => -1 > > > > > [message] => DB_DataObject Error: No Value specified for get > > > > > > > > > > 5. This works // updates the object: > > > > > > > > > > if( 1== 1){ > > > > > $e = 4; > > > > > } > > > > > $do->get($e); > > > > > > > > > > 6. This does not work: // instead creates new object (but strangely, > > > > > does NOT give the error message seen in example #4 about "No Value > > > > > specified for getdb_dataobject_error...") even though the get is > > > > > similarly outside of the if statement > > > > > > > > > > if( 1== 1){ > > > > > $e = $_GET['id'] + 0; > > > > > } > > > > > $do->get($e); > > > > > > > > > > _________________________________ > > > > > In summary: > > > > > I've tried the above and a few other things. > > > > > I am confused. > > > > > I would appreciate any insight. > > > > > > > > > > Thanks, > > > > > > > > > > s. > > > > > > > > > > -- > > > > > PEAR General Mailing List (http://pear.php.net/) > > > > > To unsubscribe, visit: > > > > > http://www.php.net/unsub.php > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > DB_DataObject_FormBuilder - The database at your fingertips > > > > > > > > http://pear.php.net/package/DB_DataObject_FormBuilder > > > > > > > > paperCrane --Justin Patrin-- > > > > > > -- > > > PEAR General Mailing List (http://pear.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > !DSPAM:40ef1815181775279997182! > > > > > > -- > DB_DataObject_FormBuilder - The database at your fingertips > http://pear.php.net/package/DB_DataObject_FormBuilder > > paperCrane --Justin Patrin--

« previous php.pear.general (#13621) next »