Re: DB_DataObject Bug?
| From: | Justin Patrin | Date: | Mon, 09 Aug 2004 16:47:25 +0000 |
| Subject: | Re: DB_DataObject Bug? | ||
| References: | 1 2 3 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-14023@lists.php.net to get a copy of this message | ||
On Mon, 09 Aug 2004 13:54:37 +0200, Verona Busch <veronabusch@gmx.de> wrote:
> Dear Justin,
>
Please send your messages to the list too so others can help and see
the solution.
First thing: have you seen these docs yet?
http://opensource.21st.de/tiki-index.php?page=DB_DataObject_FormBuilder
All docs on that site should be fully up to date.
> thanks for your message. I installed the old DataObject again.
There has been a new release that fixes the problem.
> Anyway.
> There should be a feature to reinstall older versions of PEAR packages,
> shouldn't it. Also I think, that it would be better to get all versions
> (alpha, beta, stable) shown in the PEAR Web-Interface. It doesn't make
> sense to me, to put that i an config file.
>
> But I didn't come forward with yout FormBuilder packages. I give up
> right now. I think without detailed knowledge of the different pear
> packeges and of php itself, it's to complicate to manage without an also
> detail tutorial for all the different possibilities.
>
I'm not quite sure what you mean here. I understand that tutorials
would help, but you can always look at the docs and ask us questions.
We're always happy to help, as long as your questions are
understandable.
> See my SQL-Statement for example:
>
> SELECT
>
persons.gender,
> persons.title,
> persons.firstname,
> persons.surname,
> persons.position,
> persons.notice,
> addresstyps.addresstyp,
> addresses.addresstyp_uid,
> addresses.addressee,
> addresses.mailbox,
> addresses.zipcode,
> addresses.city,
> addresses.state,
> addresses.country
> FROM
> _persons_addresses
> RIGHT OUTER JOIN persons ON
> (_persons_addresses.person_uid =
> persons.uid)
> LEFT OUTER JOIN addresses ON
> (_persons_addresses.adress_uid =
> addresses.uid)
> LEFT OUTER JOIN addresstyp ON
> (addresses.addresstyp_uid =
> addresstyp.uid)
Yep, 3 joins. That's not likely to translate into a usable form with
formbuilder. In fact, I fail to see how you'd use this in a form at
all. You're selecting the person's info for each address they have.
Why not select the person, then select the addresses for that person?
Also, why do you have an intermediate table between persons and
addresses? You're allowing one address to belong to multiple persons
this way. Is this what you want? If not, I would suggest putting a
persons_uid field in the addresses table and forgetting
_persons_addresses.
In addition, I don't believe this query will even work as you have not
told mysql that you're linking to _persons_addresses. You have it in 2
ON statements, but you're not JOINing to it anywhere.
>
> As you can see I'm fare away from an usefull from. I didn't try to let
> gender to be a radio button (or select box) and addresstyp to be a
> select box, because I already got mixed up with links and loops.
>
> $objNewPerson =& new databaseobject_Persons();
You're supposed to use DB_DataObject::factory instead of instantiating
data objects yourself.
$objNewPerson = DB_DataObject::factory('persons');
>
> /* Can't I use getAll()?
> * I need "SELECT * FROM...
> */
Not sure what you're aksing here. getAll() is a method in DB that
pulls all of the records associated with your query. You do this with
DB_DataObject by doing:
$do->find();
while($do->fetch()) {
//do something
}
>
> //$objNewPerson->get();
>
> $objNewPerson->whereAdd('surname = "Miller" /*AND firstname =
> "Steven"*/');
First of all, you're commenting *within* the query string....that
seems strange. Also, you shouldn't have to do this. Perhaps you didn't
fully read the DB_DataObject docs? Here's how you should be doing
this:
$objNewPerson->surname = 'Miller';
$objNewPerson->firstname = 'Steven';
>
> $objNewPerson->find();
>
find() will add all of the set properties of the DataObject to its query.
> while ($objNewPerson->fetch()){
>
> $objNewAddress =
>
> $objNewPerson->getLink('uid','_persons_addresses','person_uid');
This call says: get the record that 'uid' links to in the table
'_persons_addresses'. I suppose it will work, but it's a bit strange
to do it this way as you'll get only one record back.
>
> $objNewAddress =
> $objNewAddress->getLink('address_uid','addresses','uid');
Ok, you're getting a linked address. I assume for editing... With this
code you're assuming that the person has only one address?
>
> /* I can echo that, but can't put it into $form!
> * Has that to be done through preProcess?
> */
>
> echo $objNewPerson->surname . " lives in " . $objNewAddress->city;
>
> /* And what about this?
> * Person has got two addresses!
> * Where should I loop?
> */
>
> while ($objNewAddress->fetch()){
This won't work. You never did a find(), so a fetch() does nothing.
You're not understanding what the DataObject functions do. Here's what
you want to do:
$addrLink = DB_DataObject::factory('_persons_addresses');
$addrLink->person_uid = $objNewPerson->uid;
$addrLink->find();
while($addrLink->fetch()) {
//this assumes you've set up your links.ini correctly
$addr = $addrLink->getLink('address_uid');
//do something with $addr
}
>
> echo " but also in " . $objNewAddress->city;
>
> }
>
> $fg =& DB_DataObject_FormBuilder::create($objNewPerson);
> $form =& $fg->getForm();
> if ($form->validate()) {
> $form->process(array(&$fg,'processForm'), false);
> $form->freeze();
> }
> $form->display();
Ok, good, you're displaying a form for editing the person you
selected. The form will only include fields from the person, though,
as that's what it's supposed to do.
If you want ot display the person and all of their addresses in one
huge form, FormBuilder can't really do that, at least not yet. It's
meant to display a form for a *single record*. You can quite easily
display the form for the person and list all of the addresses with an
edit link for each address. Once the edit link is clicked, you can
instead show the form for the address.
>
> }
>
> Hopeless, isn't it? :-)
Not hopeless, you just have to use the components correctly.
>
> Bye Vero
>
--
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder
paperCrane --Justin Patrin--