Search code examples
mysqlcakephpdelete-rowmysql-error-1054

Do database row DeleteAll where 1 field is equal to something and other field is NOT equal to something


I'm struggling with doing this, I want to basically do a database deleteAll where one field is equal to something and another field must NOT be equal to something.. its for deleting duplicate rows so I want to delete all but one row.. the way I tried below isn't working, I Would appreciate any advice:

 $conditions = array (
  "Prox.proxy" => $currentproxytocheck,
  "AND" => array (
   "NOT" => array (
    "Prox.proxyid" => $currentproxyid
   )
  )
 );

$this->Prox->deleteAll(array( 'conditions' => $conditions)); 

EDIT:

The printout of my $conditions array is:

Array
(
    [Prox.proxy] => 62.58.179.2:80
    [AND] => Array
        (
            [NOT] => Array
                (
                    [Prox.proxyid] => 36829
                )

        )

)

Error from CAkephp:

Notice (8): Array to string conversion [CORE/cake/libs/model/datasources/dbo_source.php, line 2193]
Warning (512): SQL Error: 1054: Unknown column 'conditions' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 673]   

Solution

  • The syntax for deleteAll is different from find

    deleteAll(mixed $conditions, $cascade = true, $callbacks = false)
    

    Use

    $this->Prox->deleteAll($conditions); 
    

    And your array could be built like so:

    $conditions = array (
        "Prox.proxy" => $currentproxytocheck,
        "Prox.proxyid <>" => $currentproxyid
    );
    

    Which is the same thing, but more readable.