安装CRM Core后出现方法声明兼容致命错误求助
This error happens because PHP enforces strict method signature compatibility between parent and child classes. Your CRMCoreContactController::save() method only defines one parameter ($contact), but its parent class EntityAPIController::save() expects two: $entity and an optional nullable DatabaseTransaction parameter. Here are practical solutions to resolve this:
1. Quick Temporary Fix (Direct File Edit)
This will get your site running immediately, but note that updating the CRM Core module later will overwrite this change—so it’s a short-term fix only:
- Navigate to
/opt/lampp/htdocs/drupal/modules/crm_core/modules/crm_core_contact/includes/crm_core_contact.controller.inc - Go to line 111 where the
save()method is declared - Modify the method signature to match the parent class:
public function save($contact, ?DatabaseTransaction $transaction = NULL) { // Keep all existing code inside the method exactly as it was }
Parameter names don’t affect compatibility in PHP—only the count, types, and optional status matter. Adding the second optional parameter aligns the method with EntityAPIController::save().
2. Long-Term, Upgrade-Safe Fix (Custom Module)
To avoid modifying contributed module files (which get overwritten on updates), create a small custom module to override the controller:
- Create a new module (e.g.,
crm_core_compatibility_fix) with a.info.ymlfile and a.modulefile - Add this code to your
.modulefile:/** * Implements hook_entity_info_alter(). */ function crm_core_compatibility_fix_entity_info_alter(&$entity_info) { if (isset($entity_info['crm_core_contact'])) { $entity_info['crm_core_contact']['controller class'] = 'CRMCoreContactControllerFixed'; } } /** * Fixed controller class for CRM Core Contact entities. */ class CRMCoreContactControllerFixed extends CRMCoreContactController { /** * Override save method to match parent signature. */ public function save($entity, ?DatabaseTransaction $transaction = NULL) { // Pass through to the parent method to preserve original logic return parent::save($entity, $transaction); } } - Enable the custom module and clear your Drupal cache.
3. Root Cause Resolution (Version Compatibility)
This mismatch typically occurs when using an older CRM Core version with a newer Entity API. Check CRM Core’s project documentation to confirm version compatibility. If an updated CRM Core version exists that supports your Entity API version, upgrading is the cleanest long-term solution.
Final Step: Clear Cache
After making any changes, clear all Drupal caches to ensure the new class definitions load. If you can’t access the admin UI, use Drush:
drush cr
Or manually delete cache files in sites/default/files/php/.
内容的提问来源于stack exchange,提问作者Bernes Suljic

