0
votes

I know there is this solutions (https://drupal.stackexchange.com/questions/213379/programmatically-update-an-entity-reference-field)

But I didn't work for me and I have an error : Drupal\Core\Entity\EntityStorageException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'created' at row 1: INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => 791 [:db_insert_placeholder_1] => 10 [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => 2021-08-17T12:32:12.397 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 846 of core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).

When I'm trying to create a node with a entity reference field. The entity is a taxonomy term an they don't need to be create. I use an API call to populate my content type.

Here is my code :

$newJob = Node::create([
    'type' => 'jobs',
    'title' => $job->label,
    'uid' => 219,
    'field_api_id' => $job->id,
    'created' => $job->publishedDate,
    'field_jobs_title_function' => [
      'value'   =>  $job->label
    ],
    'field_jobs_purpose' => [
      'value'   =>  !is_object($job->JobDataSet->JobData->role)            ? $job->JobDataSet->JobData->role            : '',
      'format'  => 'full_html_no_ckeditor'
    ],
    'field_jobs_how_to_apply' => [
      'value'   =>  !is_object($job->JobDataSet->JobData->requiredFiles)   ? $job->JobDataSet->JobData->requiredFiles   : '',
      'format'  => 'full_html_no_ckeditor'
    ],
    'field_link_cta' => [
      'uri'   =>  !is_object($job->JobDataSet->JobData->applyUrl)          ? $job->JobDataSet->JobData->applyUrl        : '',
      'title' =>  'Postuler maintenant !'
      ],
  ]);

  $newJob->setPublished(TRUE);
  $newJob->save();

  $newJob->field_domain->target_id = $domain;
  $newJob->save();

$domain is an int correspondinf to the existing taxonomy term and It works when I load it with :

Term::load($domain);

But when I want to create the node I always have the error SQLSTATE[01000].

I've tried :

,
    'field_domain' => [
      'target_id' => $domain
    ],

$newJob->field_domain->target_id = $domain;

Why isn't that correct for Drupal ? Thank u

2
Is it working without adding an entity reference?akanksha-hp
No, it isn't workingb.ferdinand

2 Answers

0
votes

If the field is an entity reference, you should just have to provide a target id to link the entities to eachother. In you case the following should suffice:

$newJob = Node::create([
    ...
    'field_domain' => $domain,
    ...
])

where the $domain variable is just the id that corresponds to the other entity. There is no need to specify that this variable is the target id.

0
votes

I find the solution.

The problem was the date's format.

It has to be a timestamp and it wasn't I don't know why.

$newJob->setCreatedTime((new DrupalDateTime($newJob->getCreatedTime()))->getTimestamp());
$newJob->field_domain->setValue($domain);
$newJob->save();