Search code examples
mysqlsqldrupaldrupal-7

How can I get the actual files of a node in drupal 7 with SQL query?


I have to get datas from a drupal 7 database, but without the drupal environment, so I can't use the default drupal classes to get it, only SQL codes.

I want to get files of a node, and I wrote this code:

SELECT 
    tny_t.uri as tny,
    tny_t.filename as tny_name,
FROM ".$type."
LEFT JOIN dr_node
    ON dr_node.nid= ".$type.".entity_id
LEFT JOIN dr_field_revision_field_teljes_tm_nynyilatkozat
    ON dr_field_revision_field_teljes_tm_nynyilatkozat.entity_id=".$type.".entity_id
LEFT JOIN dr_file_managed as tny_t
    ON tny_t.fid= dr_field_revision_field_teljes_tm_nynyilatkozat.field_teljes_tm_nynyilatkozat_fid
WHERE dr_node.nid = ".$nid."

It gives me files, but not always the good ones, because in time the older files was replaced to anothers. I think this gives me the older files.


Solution

  • In your case you must use "file_usage" table who track where a file is used. File usage schema : enter image description here

    So you can use this request to see all files related to specific node :

    'SELECT * FROM file_usage fu 
    LEFT JOIN file_managed fm ON fu.fid = fm.fid  
    WHERE fu.type = "node"
    AND fu.id ='. $nid