Forums / Developer / Checking for unique attribute data in objects

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.

eZ debug

Timing: Jan 18 2025 11:04:06
Script start
Timing: Jan 18 2025 11:04:06
Module start 'content'
Timing: Jan 18 2025 11:04:07
Module end 'content'
Timing: Jan 18 2025 11:04:07
Script end

Main resources:

Total runtime0.9575 sec
Peak memory usage4,096.0000 KB
Database Queries207

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0069 588.9766180.8125
Module start 'content' 0.00700.8185 769.7891659.5469
Module end 'content' 0.82550.1319 1,429.3359341.0547
Script end 0.9574  1,770.3906 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.4608210.0002
Check MTime0.00150.1581210.0001
Mysql Total
Database connection0.00140.145310.0014
Mysqli_queries0.866190.45372070.0042
Looping result0.00210.22372050.0000
Template Total0.927296.820.4636
Template load0.00200.212220.0010
Template processing0.925196.621420.4626
Template load and register function0.00010.008910.0001
states
state_id_array0.00110.112410.0011
state_identifier_array0.00090.094920.0005
Override
Cache load0.00180.1913690.0000
Sytem overhead
Fetch class attribute can translate value0.00180.187950.0004
Fetch class attribute name0.00100.107080.0001
XML
Image XML parsing0.00160.171650.0003
class_abstraction
Instantiating content class attribute0.00000.002190.0000
General
dbfile0.00690.7242330.0002
String conversion0.00000.000830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 23
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs