I have a custom content entity (amap_distributions_inscriptions) linked to 2 entity_reference 'datedistributionid' (which is basically the date of distribution plus other fields) and 'amapienid' (which is mainly the people who can attend to the distribution).
I want to create a form allowing people to enroll to the distribution, i.e. choose a date among all possible and their role.
In the past (a module I wrote for Drupal 6), I had a db_query joining the three tables, sorting by date, name of the people, etc. I processed the result of the query with a while ($record = db_fetch_object($result))
to build a tabular form with all dates and already enrolled people and so on.
What would be the "Drupal 8" way to do that?
I started with a
$storage = \Drupal::entityManager()->getStorage('amap_distributions_inscriptions');
$ids = \Drupal::entityQuery('amap_distributions_inscriptions')
->execute();
$inscriptions = $storage->loadMultiple($ids);
foreach ($inscriptions as $id=>$inscription) {my code here}
but I'm struggling with retrieving 'date distribution' and 'amapien', then sorting the results before starting to build the form rows...
Any guidance?
Trying to use @ssibal's advice, I assume I should write something like
$database = \Drupal::database();
$result = $database
->select('amap_distributions_dates', ???)
->fields('ddate', 'iid', 'pid', 'irole', 'nom', 'prenom')
->condition('dnumber', 0, '>')
->execute()
My D6 query ('amap_distributions_dates', 'amap_distributions_inscriptions' and 'association_personnes' being the 3 tables):
$query = "
SELECT
amap_distributions_dates.ddate,
iid,
amap_distributions_inscriptions.pid,
irole,
nom,
prenom
FROM
amap_distributions_dates
LEFT JOIN amap_distributions_inscriptions ON amap_distributions_inscriptions.ddate = amap_distributions_dates.ddate
LEFT JOIN association_personnes ON association_personnes.pid = amap_distributions_inscriptions.pid
WHERE
dnumber > 0
ORDER BY
amap_distributions_dates.ddate ASC,
iid ASC,
irole ASC,
nom ASC,
prenom ASC
";