Get ClassName after eZContentObject::fetchByNodeId()

Author Message

Damien MARTIN

Tuesday 22 December 2009 2:12:13 am

Hi there,

I have a problem with eZContentObject::fetchByNodeId().

When I try to fetch a node like the 64, I use something like that (in fact it's in a loop) :

$noeud = eZContentObject::fetchByNodeId(64);

But when I want to see what it returns with :

ezDebug::writeNotice(print_r($noeud, true), "Damien");

The output shows something like :

 eZContentObject Object

(
    [ID] => 62
    [Name] => My wonderfull folder
    [CurrentLanguage] => fre-FR
    [ClassName] => 
    [ClassIdentifier] => 
    [DataMap] => Array
        (
            [3] => Array
                (
                    [fre-FR] => Array
                        (
                            [nom] => eZContentObjectAttribute Object
                                (
                                    [HTTPValue] => 
                                    [Content] => 
                                    [DisplayInfo] => 
                                    [IsValid] => 

So, why haven't I the correct node ID in the returned Object while the datas are good (DataMap, has the correct values) ?

This problem is embarassing because I need to know the ClassName whos is not defined in the returned object...

For information :
eZ Publish version 4.2.0
Installed as an ezwebin template (without content)

Thanks

Bertrand Dunogier

Tuesday 22 December 2009 3:07:38 am

First, you don't need to pass your variable through print_r, eZDebug::write* do it automatically.

Then... nothing's wrong with the returned values. You're confusing object & node ID. You're fetching the eZContentObject that matches node 64, and it happens that this object's ID is 62. Since multiple nodes can link to the same object, it is expected that they are different.

if your object only has one node, you can check that the value is correct by using:

$noeud->attribute('main_node_id');

Note that $noeud is wrong here, as the object is an object, not a node ;)

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

Damien MARTIN

Tuesday 22 December 2009 5:27:22 am

Thanks Bertrand,

Now, I understand why I haven't the same value in ID...

But I still don't understand why the ClassName attribute is empty.
Have I to do something more to get it ?

And as you said, I will correct my source code and change $noeud for something different :)

Jérôme Vieilledent

Tuesday 22 December 2009 5:46:05 am

Hi Damien
Actually, this property is not static. It means that it is filled by calling a method (attribute() in that case, which is automatically called in templates).
So, to have your class name, you should do this :

$object = eZContentObject::fetchByNodeId(64);
$className = $object->attribute('class_name');

You can find all the attributes (or properties) available with this method in the objects reference documentation. Also note that, when you find a PHP class extending eZPersistentObject in eZ Publish, just have a look in the definition() static method. You'll find here all attributes you want ;)

A little more documentation about eZPersistentObject.

Damien MARTIN

Tuesday 22 December 2009 5:54:58 am

Thanks Jérôme !

It works well !

I'll read the documentation about eZPersistentObject and the object reference documentation (I bookmarked them).

I'm sorry but I'm a beginner in writting extensions for eZ... But it's really exciting !

I will mark the topic as solved and I will change it's name for something more realistic than the current.

Thank you again Bertrand and Jérôme.

Jérôme Vieilledent

Tuesday 22 December 2009 6:00:23 am

I'm happy I could help you :)

Don't be sorry, we were all beginners once !
Anyway, I'm happy you enjoy developping eZ extensions ! You then may want to share them on the projects website ;).

Damien MARTIN

Tuesday 22 December 2009 6:19:25 am

I started to post some things : eZ Online Template Editor

But there is no "eZ code" inside, it is just template overrides.

I make extensions for my job and I hope to release them in a near futur !

Bertrand Dunogier

Tuesday 22 December 2009 2:00:47 pm

A possibly useful tip: when you look at objects reference pages on the documentation, there is a "Static" column. This indicates wether the value for this attribute is fetched by default (usually a value stored in the SQL table as is), or has to be fetched / computed when requested.

http://ez.no/doc/ez_publish/technical_manual/4_x/reference/objects/ezcontentobject

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

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