2016-11-09 3 views
0

Я борюсь с попыткой загрузить модель в компонент.loadModel() вызывает неожиданную ошибку в Cakephp 3

Я интегрирую обработку подписки Stripe в приложение Cakephp 3.3, и в настоящее время я работаю над захватом и работой с веб-камерами. Большая часть того, что я делаю, работает хорошо, однако я столкнулся с трудностями при попытке загрузить модели «Пользователи» и «Планы» в SubscriptionComponent.

Вот сообщение об ошибке возвращается в Stripe приложением, когда я отправить тестовое webhook:

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

Это сокращенная версия 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; 
} 

Это (также сокращенно) 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; 
} 
} 

Я все еще изучаю CakePhp, поэтому, пожалуйста, b Пациент со мной :-) Я уверен, что это что-то простое - просто не очевидное для меня.

Любая помощь абсолютно будет признательна! Thx

ответ

1

Попробуйте с 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

работает как шарм! Спасибо! – Arnold

Смежные вопросы