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

Delete multiple Rows with One Datawriter statement

Discussion in 'XenForo Development Discussions' started by Marcus, Aug 5, 2013.

  1. Marcus

    Marcus Well-Known Member

    Do you also use one Datawriter statement to delete multiple rows? I have a table:
    $dw->delete("apple") is actually what I want. Is it possible with xf 1.2?

    XenForo deletes its attachments as an example also in its Model:
        $db $this->_getDb();
    'attachment_id IN (' $db->quote(array_keys($attachments)) . ')'
    Last edited: Aug 5, 2013
  2. Chris D

    Chris D XenForo Developer Staff Member

    DataWriters have pretty much a static update condition. This is defined in the _getUpdateCondition() function.

    Usually this takes the form of:

    return 'some_id = ' $this->_db->quote($this->getExisting('some_id'));
    So effectively every update type procedure that the DataWriter performs - even deletions - will use the clause:

    'WHERE some_id = X'

    With that in mind, no. You cannot delete multiple rows with one DataWriter statement.

    You would either need to loop:

    $dw XenForo_DataWriter::create('YourAddOn_DataWriter_Writer');

    foreach (
    $array AS $id => $value)

    Or just write a vanilla query:

    $db XenForo_Application::getDb();
    $db->delete('table_name''some_id IN (' $db->quote($arrayOfSomeIds) . ');
    Marcus likes this.
  3. Marcus

    Marcus Well-Known Member

    Thanks for the detailled explanation. In my case I could use WHERE fruit = "apple". Can't I use this - to stay true with my example
      protected function _getUpdateCondition($tableName)
    'fruit = ' $this->_db->quote($this->getExisting('fruit'));
    I did not understand the quote() part. I updated my first post just before your answer, in XenForo they use also the Model to delete some stuff.
  4. Chris D

    Chris D XenForo Developer Staff Member

    That might work.

    Only, if you're ever going to update the country column using the DataWriter, it will set both rows to the same country.

    The quote() function escapes a string or array appropriately for use in a SQL query.

    The delete() function runs a simple delete query. The first parameter is the table. The second parameter is the WHERE clause.


    $db->delete('fruit_table''fruit = ' $db->quote('apple'));
    Translates to:

    DELETE FROM fruit_table WHERE fruit = apple
    Generally, if you're not needing to run any postDelete functions in the DataWriter, you can just use simple SQL deletes.
    Marcus likes this.

Share This Page