This seems wrong...

Robbo

Well-known member
The commented out code would run much faster as it is just one query and not creating and executing a new data writer for every image.
PHP:
public function moveImages($oldId, $newId)
    {
        if (!$this->getCategoryById($newId))
            return;

        $images = $this->getImages($oldId);
        foreach ($images as $image)
        {
            $dw = XenForo_DataWriter::create('Merc_DataWriter_GalleryLite_Image');
            $dw->setExistingData($image);
            $dw->set('cat_id', $newId);
            $dw->save();
        }

        /*$this->_getDb()->query('
            UPDATE merc_gallerylite_image
            SET cat_id = ? WHERE cat_id = ?',
            array($newId, $oldId)
        );*/
    }

Is there some way I can do this with the data writers? Or should I just be using that query?

I don't care if this code is slow as it will rarely be executed but was just wondering anyway.
 
Change your foreache to be:
PHP:
$dw = XenForo_DataWriter::create('Merc_DataWriter_GalleryLite_Image');
foreach($images as $image)
{
    $dw->setExistingData($image);
    $dw->set('cat_id', $newId);
    $dw->save();
}

I don't think you are able to multiple images at the same time. However, if you are just updating ALL images with the category, this should work:
PHP:
$this->_getDb()->query('
            UPDATE merc_gallerylite_image
            SET cat_id = ? WHERE cat_id = ?',
            $newId, $oldId
        );
 
Yes I know the query will work but wasn't sure if there was some kind of unspoken rule about doing updates, deletes, inserts without using the datawriter.
 
Yes I know the query will work but wasn't sure if there was some kind of unspoken rule about doing updates, deletes, inserts without using the datawriter.
Generally speaking, don't. There are occasions when we avoid using a datawriter, but they are very few and far between.

One thing you might want to look into is deferred rebuilds, so that you only rebuild the category information once, after all the images have been updated. Take a look at how we use OPTION_POST_WRITE_UPDATE_USER_FOLLOWING in XenForo_DataWriter_Follower.
 
Generally speaking, don't. There are occasions when we avoid using a datawriter, but they are very few and far between.
I was wondering about this for a while. I don't use a datawriter if I'm updating a table that holds a many to many relationship (where the primary key is usually spanning two columns or more). Didn't really take the time to look into how to do that. I also don't use a datawriter when using syntax such as "UPDATE ... ON DUPLICATE KEY UPDATE". Are these valid cases for not using a datawriter, or is there a way to use the datawriter in these situations?
 
Generally speaking, don't. There are occasions when we avoid using a datawriter, but they are very few and far between.

One thing you might want to look into is deferred rebuilds, so that you only rebuild the category information once, after all the images have been updated. Take a look at how we use OPTION_POST_WRITE_UPDATE_USER_FOLLOWING in XenForo_DataWriter_Follower.
Yeah I have used options like that in the past, won't really work in this situation I don't think?

Anyways, from my first post would this be a situation where you avoid the datawriter and just use a query? Or should I leave it how it is (except move creating the datawriter out of the loop).
 
Yeah I have used options like that in the past, won't really work in this situation I don't think?

Anyways, from my first post would this be a situation where you avoid the datawriter and just use a query? Or should I leave it how it is (except move creating the datawriter out of the loop).
You know best how your code will operate - if there is no denormalised data that needs to be updated or rebuilt, and there are no events in your code that would allow denormalised data to be generated in the future, it should be safe to bypass the DW.
 
Top Bottom