CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Read files (SQL) using API in PHP for an Extension
Pages: [1]

Author Topic: Read files (SQL) using API in PHP for an Extension  (Read 282 times)

lee.gooding

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
    • Clear River Church
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.7
  • PHP version: 5.3
Read files (SQL) using API in PHP for an Extension
February 09, 2015, 10:07:37 am
I'm trying to read in an SQL query using the API. I'm using file_get_contents();

In my extension I have a directory named sql. I would like to load files from that directory. How do I tell my PHP API to where to load that file? Simply setting sql/file.sql does not work.

Thanks!

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Read files (SQL) using API in PHP for an Extension
February 09, 2015, 10:16:03 am
Take a look at https://github.com/civicrm/civicrm-core/blob/4.5.0/CRM/Utils/File.php#L274 , e.g.

Code: [Select]
    CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $fileName);

The $fileName should be relative to the extension's folder. You can usually figure this out using __DIR__ (e.g. "__DIR__ . '/sql'" or "dirname(__DIR__) . '/sql'").

lee.gooding

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
    • Clear River Church
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.7
  • PHP version: 5.3
Re: Read files (SQL) using API in PHP for an Extension
February 09, 2015, 10:46:30 am
This is helpful.

Unfortunately dirname(__DIR__) gives me the pathToExtension/api/v3.

To deal with this I had to use the following:
dirname(dirname(dirname(__DIR__)));

That gives me the root path of my extension.

Thanks for the quick response!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Read files (SQL) using API in PHP for an Extension

This forum was archived on 2017-11-26.