Answers.Body: <p>One thing you can try is invoking the service and initializing it from there. That will give you access to API4.</p>
  <pre><code>$civicrm = \Drupal::service('civicrm');
  $civicrm-&gt;initialize();
  </code></pre>
  <p>Alternatively -
  <code>\Drupal::service('civicrm')-&gt;initialize();</code></p>
  <p>Additionally, you can use the following to access API related classes:</p>
  <pre><code>use Drupal\civicrm\Civicrm;
  use Civi\Api4\Contact;
  use Civi\Api4\Membership;
  use Civi\Api4\Address;
  use Civi\Api4\Phone;
  use Civi\Api4\Email;
  
  //in function
  $memberships = Membership::get(FALSE)
    -&gt;addSelect...........
  $contacts = Contact::get(FALSE)
    -&gt;addSelect.........
  
  //etc
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-31T03:54:42.950Z
Answers.Text: `\Drupal::service('civicrm')->initialize();` did the trick, thanks.
Answers.UserDisplayName: krystalcode
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-25T17:54:07.283Z
Answers.LastActivityDate: 2023-01-25T17:54:07.283Z
Answers.UserDisplayName: Greyson
Body: <p>I have CiviCRM 5.56.1 installed via composer on a Drupal 9 application. In a custom module I use API v4 to manipulate records. For example, get membership types.</p>
  <pre><code>\Civi\Api4\MembershipType::get()-&gt;addWhere('name', 'My Membership')-&gt;execute();
  </code></pre>
  <p>The error that I get is the following:</p>
  <pre><code>Error: Class &quot;CRM_Core_DAO&quot; not found in /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Utils/CoreUtil.php on line 101 #0 /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Generic/DAOGetAction.php(156): Civi\Api4\Utils\CoreUtil::getOperators()
  </code></pre>
  <p>In my civicrm settings file I have made sure to add the correct <code>$civicrm_root</code> variable. I have check the resulting include paths and the correct civicrm core path is included. CiviCRM in general works fine via the UI. The same command works when using the <code>cv</code> CLI.</p>
  <p>Any ideas why this happens when calling from within Drupal? It seems namespaced classes are found. In older related questions it is suggesting to use <code>civicrm_initialize()</code>, but this function does not seem to exist anymore.</p>
  
Tags: <autoloading>
Title: Class "CRM_Core_DAO" not found
ThreadText: # Class "CRM_Core_DAO" not found
  
  (Asked by: username_0 on 2023-01-25)
  
  I have CiviCRM 5.56.1 installed via composer on a Drupal 9 application. In a custom module I use API v4 to manipulate records. For example, get membership types.
  
  ```
  \Civi\Api4\MembershipType::get()->addWhere('name', 'My Membership')->execute();
  
  ```
  
  The error that I get is the following:
  
  ```
  Error: Class "CRM_Core_DAO" not found in /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Utils/CoreUtil.php on line 101 #0 /var/www/html/vendor/civicrm/civicrm-core/Civi/Api4/Generic/DAOGetAction.php(156): Civi\Api4\Utils\CoreUtil::getOperators()
  
  ```
  
  In my civicrm settings file I have made sure to add the correct `$civicrm_root` variable. I have check the resulting include paths and the correct civicrm core path is included. CiviCRM in general works fine via the UI. The same command works when using the `cv` CLI.
  
  Any ideas why this happens when calling from within Drupal? It seems namespaced classes are found. In older related questions it is suggesting to use `civicrm_initialize()`, but this function does not seem to exist anymore.
  
  **Tags:** autoloading
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-01-25. Score: 1 | Accepted answer
  
  One thing you can try is invoking the service and initializing it from there. That will give you access to API4.
  
  ```
  $civicrm = \Drupal::service('civicrm');
  $civicrm->initialize();
  
  ```
  
  Alternatively -
  `\Drupal::service('civicrm')->initialize();`
  
  Additionally, you can use the following to access API related classes:
  
  ```
  use Drupal\civicrm\Civicrm;
  use Civi\Api4\Contact;
  use Civi\Api4\Membership;
  use Civi\Api4\Address;
  use Civi\Api4\Phone;
  use Civi\Api4\Email;
  
  //in function
  $memberships = Membership::get(FALSE)
    ->addSelect...........
  $contacts = Contact::get(FALSE)
    ->addSelect.........
  
  //etc
  
  ```
  
  > **Comments:**
  > **username_0**: `\Drupal::service('civicrm')->initialize();` did the trick, thanks.  
  
  ---
  
  