mariosimao / notion-sdk-php

A complete Notion SDK for PHP developers.
https://mariosimao.github.io/notion-sdk-php/
MIT License
150 stars 24 forks source link

[FEATURE] Delete page properties #257

Closed Berdir closed 1 year ago

Berdir commented 1 year ago

Is your feature request related to a problem? Please describe.

First I thought being able to delete properties is only missing as a dedicated PHP method, but even doing the following does nothing, so I guess properties that aren't being sent are not removed:

if ($delete_properties) {
      $all_properties = $instance_page->properties;
      $all_properties = array_diff_key($all_properties, array_flip($delete_properties));
      $instance_page = $instance_page->changeProperties($all_properties);
    }

Setting the value to empty apparently also doesn't work, resulting in the following errors:

Uncaught PHP Exception Notion\Exceptions\ApiException: "body failed validation. Fix one:
body.properties.NAME.url should be populated or `null`, instead was `""`.

Nor is it possible to pass NULL as the value, as it is type hinted on string.

Describe the solution you'd like

Support NULL values? add removeProperty() methods? Not sure how it needs to work on the API level against Notiion.

mariosimao commented 1 year ago

Hi @Berdir, sorry for the late response. The property you are trying to delete is of type URL? If yes, indeed there is no possible way to delete yet.

mariosimao commented 1 year ago

@Berdir I have just released v1.10. It includes new methods for creating and updating page properties with empty values.

There are now two possible ways to clear the value of a property:

  1. Replace the property by an empty one.
$page = $notion
    ->pages()
    ->find($pageId)
    ->addProperty("Property name", Url::createEmpty());

$notion->pages()->update($page);
  1. Or get the property, clear it and update.
$page = $notion->pages()->find($pageId);

$property = $page->properties()->getUrl("Property name");
$property = $property->clear();

$page = $page->addProperty("Property name", $property);

$notion->pages()->update($page);
Berdir commented 1 year ago

Hi. Thanks, I'd consider that very fast feedback, not slow. Will test this.