0

I need to pass a null value for a database column, according to the code below:

$birthdate = null;

User::update()
->set('name', $name)
->set('email', $email)
->set('birthdate', $birthdate)                
->where('id', $id)
->execute();

But the error is generated:

enter image description here

How to do this ?

Note: The update is a method of clancats hydrahon.

I believe that the problem occurs in that part of the library code.

public function set($param1, $param2 = null)
{
// do nothing if we get nothing
if (empty($param1))
{
   return $this;
}
        
// when param 2 is not null we assume that only one set is passed
// like: set( 'name', 'Lu' ); instead of set( array( 'name' => 'Lu' ) );
if ( !is_null( $param2 ) )
{
   $param1 = array( $param1 => $param2 );
}
        
// merge the new values with the existing ones.
$this->values = array_merge( $this->values, $param1 ); 
        
// return self so we can continue running the next function
return $this;
}

2 Answers2

2

First of all; Never ever changes the files in the vendor directories. Vendor directories should not be versionned in your project and any changes will not be available in production or for someone else.

As answer; I think using an array should works. And also because it's wrote in comment

// when param 2 is not null we assume that only one set is passed
// like: set( 'name', 'Lu' ); instead of set( array( 'name' => 'Lu' ) );

$birthdate = null;

User::update()
->set('name', $name)
->set('email', $email)
->set(['birthdate' => $birthdate]) // like this               
->where('id', $id)
->execute();

Reason

if (empty($param1)) { // false: because it's not empty. We pass on
   return $this;
}
if ( !is_null( $param2 ) ) { // false: because it IS null. We pass on
   $param1 = array( $param1 => $param2 );
}

At this point we have $param1 = ['birthdate' => null] and $param2 = null But this time, we don't have a string. We have an array.

 // $this->values = array_merge( [], ['birthdate' => null] ); 
    $this->values = array_merge( $this->values, $param1 ); 

Going further, I think you can even do like this


User::update()
->set([
    'name'      => $name,
    'email'     => $email,
    'birthdate' => $birthdate,
 ])             
->execute();
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
1

This is because you pass null as $birthdate. That code is designed for these inputs:

$param1 = 'name';
$param2 = 'value';

or an array.

$param1 = [ 'name' => 'value' ];
$param2 = null;

You must send something as a value.

You should do it either

User::update()
->set('name', $name)
->set('email', $email)
->where('id', $id)
->execute();

or

$birthdate = 'YYYY-MM-DD'; //some date, can't be null

User::update()
->set('name', $name)
->set('email', $email)
->set('birthdate', $birthdate)                
->where('id', $id)
->execute();

or

$params = [
     'name' => $name,
     'email' => $email
];

if(!is_null($birthdate)) {
    $params['birthdate'] = $birthdate;
}

User::update()
->set($params)              
->where('id', $id)
->execute();
Jsowa
  • 9,104
  • 5
  • 56
  • 60