albert
albert

Reputation: 4468

How to set field to null on doctrine migrations using addSql?

I'm updating an entity in one of my doctrine migrations with Doctrine\DBAL\Migrations\AbstractMigration::addSql($sql, array $params = array(), array $types = array()), I have to set up a column as null.

I have already debugged it and I tried to set up the type as \PDO::PARAM_NULL but I'm getting the same issue.

SQL:

    $sql="INSERT INTO sme_task_template (status_change_final_status) VALUES (:finalStatus)";
    $params=array(
        'finalStatus' => null
    );
    $types=array(
        'finalStatus' => \PDO::PARAM_NULL
    );
    $this->addSql($sql, $params, $types);

Exception:

  [Doctrine\DBAL\DBALException]                                                
  An exception occurred while executing 'INSERT INTO sme_task_template (statu  
  s_change_final_status) VALUES (:finalStatus)' with params [null]:            
  Value for :finalStatus not found in params array. Params array key should b  
  e "finalStatus"

Does anyone know how to solve this?

Thanks

Upvotes: 1

Views: 2556

Answers (2)

Jarik Voroniak
Jarik Voroniak

Reputation: 29

Why would you do that by addSql at all? AddSQL is mainly for changing schema. What you need is postUp method and executeQuery.

public function postUp(Schema $schema): void
{
    $this->connection
        ->prepare("UPDATE `my_table` SET `my_field`=:my_value WHERE `other_field` IS NOT NULL")
        ->executeQuery(['my_value' => 'wheee!']);
}

Upvotes: 0

albert
albert

Reputation: 4468

I can't get it working with named parameters but it seems to work with question mark parameters.

    $sql="INSERT INTO sme_task_template_trigger (status_change_final_status) VALUES (?)";
    $params=array(
        null
    );
    $types=array(
        \PDO::PARAM_NULL
    );
    $this->addSql($sql, $params, $types);

Upvotes: 1

Related Questions