Problems setting item properties

vlad_b

Joined: 2006-08-18
Posts: 10
Posted: Sun, 2006-09-03 19:48

Hello, I'm having a problem adding data to an image item after I add it to an album. In the following code, I add a new image (this succeeds), then try to set its creation time and title: this fails. The image has no title, and the default creation time. Does anybody know what I'm doing wrong, or how I can get more information on this problem?

        mysql_query("BEGIN");

        GalleryLockHelper_simple::acquireWriteLock($newGalId);

        list ($ret, $newImg) = GalleryCoreApi::addItemToAlbum($oldImgPath,
                                                       $img,
                                                       '',
                                                       '',
                                                       '',
                                                       "image/jpeg",
                                                       $newGalId,
                                                       FALSE);

        if ($ret && $ret->getErrorCode())
        {
            die("Error creating photo item: " . $ret->getAsText());
        }

        list($ret,$newImg) = GalleryCoreApi::loadEntitiesById($newImg->getId());
        if ($ret)
        {
            die("Error loading new image by Id");
        }

        $newImg->setCreationTimestamp($oldGalTimestamp);
        $newImg->setTitle($img);

        $newImg->save();

        GalleryLockHelper_simple::releaseLocks($newGalId);
        mysql_query("COMMIT");
 
vlad_b

Joined: 2006-08-18
Posts: 10
Posted: Thu, 2006-09-07 23:28

Whoa, don't know what happened to the huge code there.
Anyway, I still don't know what was wrong with the original code, but I did find a workaround - by basically copying the AddItemToAlbum() code. The new code that works in setting the title and other properties does the following (truncated for clarity, by looking at the APIs the flow should be clear):
1. newItemByMimeType
2. aquire lock, newItem->create(...), releaseLocks
3. set item properties
4. $newItem->save()
5. addExistingItemToAlbum(...)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2006-09-10 18:47

i changed your post to use [ code ] instead of [ php ] (without spaces) so the font isn't so big now.

1) you're skipping a lot of error checks.. easier to find out what went wrong if you check for errors after each API call that returns an error status
2) releaseLocks takes a lock id not an item id.. you should be saving the returned lockId from acquireWriteLock and giving that to releaseLocks
3) you only need a read lock to add a new item to an album
4) not sure why you reload the item right after you create it
5) you probably need a write lock on the new item in order to save additional changes to it

your alternate set of api calls sounds fine too.