For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2006 > Re: [PEAR] FormBuilder help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] FormBuilder help
Justin Patrin

2006-05-25, 4:00 am

On 5/24/06, Patrick Paul <ppaul_apache@yahoo.ca> wrote:
> Hi,
>
> Here is what I am attempting to do using FormBuilder. I have two tables,
> cities (cities_id INT auto_increment) and cities_description (cities_id,
> languages_id, cities_name).
>
> I want FormBuilder to make a form that will add a new entry to cities
> and cities_description. The trick I'm trying to use is to have two forms
> in one, as in the rapidprototyping-kr-1.pdf. What I would like it to do
> is create a new entry in cities first, and use the resulting cities_id
> to create the new entry in cities_description.
>
> Here is my code :
>
> <?php
>
> $description =3D DB_DataObject::factory('cities_descripti
on');
> /// this is because (cities_id,languages_id) is the primary key/
> $description->keys('id');
> $city =3D DB_DataObject::factory('cities');
> $city->createSubmit=3D*false*;
>
> $descriptionBuilder =3D& DB_DataObject_FormBuilder::create($descr
iption=

);
> $cityBuilder =3D& DB_DataObject_FormBuilder::create($city)
;
> $cityBuilder->elementNamePrefix =3D 'city';
>
>
> $cityForm =3D& $cityBuilder->getForm();
> $descriptionBuilder->elementNamePrefix =3D 'description';
> $descriptionBuilder->useForm($cityForm);
> $combinedForm =3D& $descriptionBuilder->getForm();
>


Yep, the above code is correct for combining 2 FB forms into one form.

> /// Create a FormBuilder for the post/
> $formBuilder =3D &DB_DataObject_FormBuilder::create( $description );
>
> /// Actually fetch the Form/
> $form =3D &$formBuilder->getForm( );
>
> /// If we validate, then we process it here/
> if( $form->validate( ) ) {
> $form->process( array( &$formBuilder, 'processForm' ), *false* );
> /// Redirect/
> header( "Location: http://" . $_SERVER[ 'HTTP_HOST' ] .
> dirname( $_SERVER[ 'PHP_SELF' ] ) . "/test4.php" );
> }
>
> $form->display( );


But then you negate all of that here. You're using a description-only
form from my last comment to here. That's not what you want. Instead
of using $form, use $combinedForm. Then, to process, call
$form->process with *both* of the formBuilder instances. First the
city one, then the description one. Make sure to set the city ID in
the description DO before you process it, though.

> *?>*
>
>
> Any help would be welcome.
>
> Thank you,
>
> Patrick
>
> --
> PEAR General Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--=20
Justin Patrin
Patrick Paul

2006-05-25, 4:00 am

First of all thank you for you quick response.

I just subsribed to the mailing list a few hours ago and I'm receiving
any mails and I don't know why.

Anyways, regarding the problem at hand, I did what you told me and it
works to create a new city in the cities table.
However, the id used for the description is not set right and I'm not
sure I understand what you meant by "Make sure to set the city ID in
the description DO before you process it, though." What I want is the
citites_id that has just been created for the cities table.

Thanks again,

Patrick

Justin Patrin wrote:

>
> But then you negate all of that here. You're using a description-only
> form from my last comment to here. That's not what you want. Instead
> of using $form, use $combinedForm. Then, to process, call
> $form->process with *both* of the formBuilder instances. First the
> city one, then the description one. Make sure to set the city ID in
> the description DO before you process it, though.
>
>
>

Patrick Paul

2006-05-25, 7:01 pm

Thank you Justin, everything is working great.

> Oh, and don't do this:
>
> Give your description table its own auto_inc PK.


I'm guessing this is because Data_Object does not support multi-field
keys ? Is there any way to go around this other than adding an auto_inc
PK to tables ? (I have 70 tables). Is it really that bad to do a
"$description->keys('id');" ?

Thank you,

Patrick

Justin Patrin wrote:

> On 5/24/06, Patrick Paul <ppaul_apache@yahoo.ca> wrote:
>
>
>
> Yes, as I said:
> $description->cities_id = $city->cities_id;
> Oh, and don't do this:
>
>
> Give your description table its own auto_inc PK.
>
>
>

Patrick Paul

2006-05-25, 7:01 pm

Justin Patrin wrote:

> On 5/25/06, Patrick Paul <ppaul_apache@yahoo.ca> wrote:
>
>
>
> There is no 'id' field in this table, so this is incorrect.



So I guess my question is the following : is there any proper way to
make DO understand that there is no primary key so that it won't give
them a "special treatment" that it incompatible with my code ?

If not, how could I work around this limitation, or maybe modify DO to
my needs ? (preferably without creating a useless auto incremented PK +
UNIQUE index on my 2 fields)

Thank you,

Patrick

P.S. I am now receiving messages from the mailing list, so you don't
need to cc anymore :-)

>
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com