php - How to excute a query in Drupal 8?
Get the solution ↓↓↓I want something like :SELECT tid, name FROM taxonomy_term_field_data where vid = 'copyright' and tid not in (SELECT DISTINCT field_term_copyright_target_id FROM media__field_term_copyright);
My Code:
<?php
$query_unused_taxonomy = "
SELECT
tid, name FROM {taxonomy_term_field_data}
WHERE
vid = 'copyright'
AND
tid
NOT IN
(SELECT DISTINCT field_term_copyright_target_id FROM {media__field_term_copyright});";
$database = \Drupal::database();
$query = $database->query($query_unused_taxonomy);
if ($result = $query->execute()) {
foreach ($result as $tid) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$entities = $term->loadMultiple($tid);
$term->delete($entities);
}
return ('Delete successful');
}
else {
throw new UpdateException('Query failed to execute.');
}
When i run this code the foreach loop doesn't load any results.
What can i do ?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.