Re: Weird $_GET behaviour with DB_DataObject_FormBuilder
| From: | Justin Patrin | Date: | Fri, 09 Jul 2004 21:47:45 +0000 |
| Subject: | Re: Weird $_GET behaviour with DB_DataObject_FormBuilder | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-13585@lists.php.net to get a copy of this message | ||
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
>
> !DSPAM:40ef0e04158051436513693!
>
>
--
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder
paperCrane --Justin Patrin--