Forums / Setup & design / fetch Information Collection information
Michael Scofield
Thursday 26 June 2008 6:37:01 pm
I have a form (using information collection) that collects the e-mail of the internaut.
I would like to query the database (inside PHP code, not in template code) to know if a determined e-mail address is already collected.
What's the best way to do that?
Should I use eZPersistentObject::fetchObjectList()? If so, how?
Thank youMichael
Piotrek KaraĆ
Thursday 26 June 2008 10:01:27 pm
For (almost) any db table (set) there is a corresponding dedicated PHP class extending eZPersistentObject, providing API for all the key operations on this particular dataset. If you want to be consistent, use that. For collected info, you have eZInformationCollection and eZInformationCollectionAttribute classes. You can extend them to write your own fetches or direct SQL queries - and it would be your decision which ones you use and for what purpose.
Much as I appreciate the idea behind quick and general purpose information collection, in most cases I find dedicated tables and classes encapsulated within custom extension a better choice.
Cheers,Piotrek
-- Company: mediaSELF Sp. z o.o., http://www.mediaself.pl eZ references: http://ez.no/partners/worldwide_partners/mediaself eZ certified developer: http://ez.no/certification/verify/272585 eZ blog: http://ez.ryba.eu
Thursday 26 June 2008 10:20:05 pm
I just did it.
I will write the code I used in hope that it will help someone:
// gets DB instance $db = eZDB::instance(); // SQL $sql = " SELECT count( ezinfocollection_attribute.id ) as count FROM ezinfocollection_attribute WHERE ezinfocollection_attribute.contentobject_attribute_id = '" . $contentObjectAttribute->attribute('id') . "' AND ezinfocollection_attribute.contentclass_attribute_id = '" . $contentObjectAttribute->attribute('contentclassattribute_id') . "' AND ezinfocollection_attribute.data_text LIKE '" . addslashes($email) . "' "; // executes query $resArray = $db->arrayQuery( $sql ); // verifies if the e-mail was found if (intval($resArray[0]['count']) > 0) { // found } // not found
Michael