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.