Checking for unique attribute data in objects

Author Message

Eirik Alfstad Johansen

Saturday 13 December 2003 6:53:36 am

Hi,

I'm creating a datatype to handle affiliate ID's, and as a part of the validation process I need to make sure that there doesn't already exist an object with the same affiliate id. What's the easiest way to do this?

I've studied the ezuser dataype which checks to make sure that both the username and email address are unique, and I've noticed that it makes use of the fetchObject function of the eZPersistentObject class, but I don't understand how to construct a a definition structure for the function, which is its first parameter.

Also, wouldn't it be an idea to have a check box labeled "Unique" for the existing ezp datatypes where such an option would apply? Just a thought.

Sincerely,

Eirik Johansen
Netmaking AS

http://www.netmaking.no/

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Georg Franz

Sunday 14 December 2003 5:27:20 pm

Hi Erik,

I've had a similar problem too. I needed unique textstrings.

I've created a new datatype (a copy of ezstring). My idea was: Just use the ezsearch::search to check if an attribute-value is already in use. But that didn't work, because the search-method always checks the current-user for the limitation list.

My second thought: Make an own table like the ezuser table and store there the unique strings ... not a good idea.

My third thought: Just make a query in ezcontentobject_attribute and look if the given text is there already.

So in my new datatype, I've created following code to check, if an attribute exists:

(in the method validateObjectAttributeHTTPInput)

$db =& eZDB::instance();
$db->setIsSQLOutputEnabled( false );
$query =
"SELECT
ezcontentobject_attribute.id
FROM
ezcontentobject_attribute,
ezcontentobject_version
WHERE
LOWER(ezcontentobject_attribute.data_text) = '".$db->escapeString(strtolower($data))."'
AND ezcontentobject_attribute.id <> ".$contentObjectAttribute->attribute( 'id' )."
AND ezcontentobject_attribute.contentclassattribute_id = ".$classAttribute->attribute( "id" )."
AND ezcontentobject_attribute.contentobject_id <> ".$contentObjectAttribute->attribute("contentobject_id")."
AND ezcontentobject_attribute.contentobject_id = ezcontentobject_version.contentobject_id
AND ezcontentobject_version.status = ".EZ_VERSION_STATUS_PUBLISHED."
GROUP BY ezcontentobject_attribute.id";
echo $query;
$existsArray = $db->arrayQuery( $query );

if ($existsArray != NULL or count($existsArray) > 0)
{
$contentObjectAttribute->setValidationError( ezi18n( 'extension/classes/datatypes',
'Already in use. Please try another one' ) );
}

I am not happy making that query (so I think there must be another way) - has anyone a suggestion?

But it works ...

By the way: Creating a new datatype is always an "exciting adventure", although there are tutorials in the documentation area of ez. I've always the feeling like falling in a black hole ;-)

Kind regards,
Emil.

Best wishes,
Georg.

--
http://www.schicksal.com Horoskop website which uses eZ Publish since 2004

Paul Forsyth

Monday 15 December 2003 1:14:24 am

I'll upload a category datatype to pubsvn later today, which should help you see how to use eZPersistantObject.

This datatype uses a new a database table so the object extending the eZPO is used to interact with it.

paul

Paul Forsyth

Monday 15 December 2003 7:38:57 am

Its released now:

http://ez.no/developer/ez_publish_3/contributions/category_datatype_pubsvn

The comments need improving/writing :) Hopefully you can see what is going on.

If you have questions just ask.

paul

Eirik Alfstad Johansen

Wednesday 21 January 2004 3:18:31 am

Thanks guys,

I ended up using Emil's code, and it worked like a charm.

Sincerely,

Eirik Johansen

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Joel Hardi

Monday 03 May 2004 8:57:40 pm

Thanks Emil ... it would have taken me a while to come up with that query.

For what it's worth, I second Eirik's vote for a "unique" or "allow duplicates" checkbox for datatypes when creating new content classes. Or alternately, some kind of ->is_unique() method that could be called from children of eZDateType when creating new datatypes.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 01:14:11
Script start
Timing: Jan 31 2025 01:14:11
Module start 'layout'
Timing: Jan 31 2025 01:14:11
Module start 'content'
Timing: Jan 31 2025 01:14:11
Module end 'content'
Timing: Jan 31 2025 01:14:11
Script end

Main resources:

Total runtime0.0236 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0042 588.2656151.2266
Module start 'layout' 0.00420.0031 739.4922220.7188
Module start 'content' 0.00730.0148 960.21091,005.8359
Module end 'content' 0.02210.0014 1,966.046937.9922
Script end 0.0235  2,004.0391 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002410.0782140.0002
Check MTime0.00114.4898140.0001
Mysql Total
Database connection0.00073.029910.0007
Mysqli_queries0.00187.561230.0006
Looping result0.00000.038410.0000
Template Total0.00104.410.0010
Template load0.00083.534810.0008
Template processing0.00020.848810.0002
Override
Cache load0.00062.486710.0006
General
dbfile0.003514.915080.0004
String conversion0.00000.017240.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs