2016-11-09 1 views
0

Ich habe Probleme mit dem Versuch, ein Modell in eine Komponente zu laden.loadModel() wirft unerwarteten Fehler in CakePHP 3

Ich integriere Stripe Subscription-Handling in eine CakePHP 3.3 App und arbeite derzeit an der Erfassung und dem Umgang mit Webhooks. Das meiste von dem, was ich tue, funktioniert gut, aber ich stoße in Schwierigkeiten, wenn ich versuche, die Modelle "Benutzer" und "Pläne" in die Subskriptions-Komponente zu laden.

Hier ist die Fehlermeldung von der App Stripe zurückgegeben, wenn ich einen Test Webhook senden:

<response> 
    <message>Call to undefined method App\Controller\Component\SubscriptionComponent::loadModel()</message> 
    <url>/subscriptions/stripeHook</url> 
    <code>500</code> 
</response> 

Dies ist eine gekürzte Version des SubscriptionsController:

namespace App\Controller; 

use App\Controller\AppController; 
use App\Controller\Component; 
use App\Controller\Users; 
use App\Model\Table\PlansTable; 
use App\Model\Table\UsersTable; 
use App\Model\Entity\Plan; 
use App\Model\Entity\User; 
use Cake\Core\Configure; 
use Cake\Event\Event; 
use Cake\Mailer\MailerAwareTrait; 
use Cake\ORM\TableRegistry; 
use Cake\Utility\Inflector; 

class SubscriptionsController extends AppController 
{ 
use MailerAwareTrait; 

public function beforeFilter(Event $event) 
{ 
    parent::beforeFilter($event); 
    // allow access to stripeHook without needing a user to be logged in 
    $this->Auth->allow(['stripeHook']); 

    // $this->viewBuilder()->layoutPath('App')->layout('default'); 
} 

public function stripeHook() 
{ 
    if ($this->request->is('post')) 
    { 
     \Stripe\Stripe::setApiKey(Configure::read('Stripe.secret')); 
     $this->autoRender = false; 
     $input = @file_get_contents('php://input'); 
     $event = json_decode($input); 
     try 
     { // Check against Stripe to confirm that the ID is valid 
      $event = \Stripe\Event::retrieve($event->id); 
      $handlerName = Inflector::variable(str_replace(".", "_", $event->type)) . "Hook"; 
      if (method_exists($this, $handlerName)) 
      { 
       $status = $this->$handlerName($event); 
       $log_message = 'StripeHOOK: ' . $status . var_export($event); 
       $this->log($log_message,'info'); 
       echo var_dump($status); 
      } 
      else 
      { 
       echo 'Not interested: ' . $handlerName; 
      } 
     } 
     catch (Exception $e) 
     { 
      $log_message = 'StripeHOOK - No event found for: ' . $event->id; 
      $this->log($log_message,'info'); 
     } 
    } 
    http_response_code(200); 
} 

public function customerSubscriptionCreatedHook($event) 
{ 
    return $this->customerSubscriptionUpdatedHook($event); 
} 

public function customerSubscriptionUpdatedHook($event) 
{ 
    $this->loadComponent('Subscription'); 
    $status = false; 
    $result = $this->Subscription->updateSubscription($event->data->object); 
    return $status; 
} 

Dies ist die (auch abgekürzt) SubscriptionComponent:

namespace App\Controller\Component; 

use App\Model\Table\PlansTable; 
use App\Model\Table\UsersTable; 
use Cake\Controller\Component; 
use Cake\Controller\ComponentRegistry; 

class SubscriptionComponent extends Component 
{ 
protected $_defaultConfig = []; 

public function updateSubscription($stripePlan) 
{ 
    // 
    // @NOTE: This appears to be where the error is raised 
    // 
    $this->loadModel('Plans'); 
    $this->loadModel('Users'); 
    $status = false; 
    $customer = $stripePlan->customer; 
    $plan = $stripePlan->plan->id; 
    $user = $this->Users->find('all', ['conditions' => ['User.stripe_id' => $customer]]); 
    $user = $user->first(); 
    $plan = $this->Plans->find('all', ['conditions' => ['Plan.stripe_id' => $plan]]); 
    $plan = $plan->first(); 
    return $status; 
} 
} 

Ich lerne immer noch CakePhp, also bitte b Der Patient mit mir :-) Ich bin sicher, das ist etwas Einfaches - nur nicht offensichtlich für mich.

Jede Hilfe wird sehr geschätzt werden! Thx

Antwort

1

Versuchen mit TableRegistry

namespace App\Controller\Component; 
... 

use Cake\ORM\TableRegistry; // <---- 

class SubscriptionComponent extends Component 
{ 
    protected $_defaultConfig = []; 

    public function updateSubscription($stripePlan) 
    { 
     $this->Users = TableRegistry::get('Users'); // <---- 
     $this->Plans = TableRegistry::get('Plans'); // <---- 

     ... 

     $user = $this->Users->find('all', ['conditions' => ['User.stripe_id' => $customer]]); 
     $user = $user->first(); 
     $plan = $this->Plans->find('all', ['conditions' => ['Plan.stripe_id' => $plan]]); 
     $plan = $plan->first(); 
     ... 
    } 
} 
+0

wirkt wie ein Zauber! Danke! – Arnold