• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

This seems wrong...

Robbo

Well-known member
#1
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.
 

Jeremy

Well-known member
#2
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
        );
 

Robbo

Well-known member
#3
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.
 

Kier

XenForo Developer
Staff member
#4
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.
 

Despair

Active member
#5
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?
 

Robbo

Well-known member
#6
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).
 

Kier

XenForo Developer
Staff member
#7
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.