loadModel()在Cakephp 3中引发意外错误

loadModel()在Cakephp 3中引发意外错误,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我正在努力将模型加载到组件中 我正在将条带订阅处理集成到Cakephp 3.3应用程序中,目前正在捕获和处理Webhook。我正在做的大部分工作都很好,但是当我尝试在SubscriptionComponent中加载“用户”和“计划”模型时遇到了麻烦 以下是我发送测试webhook时应用程序返回到Stripe的错误消息: <response> <message>Call to undefined method App\Controller\Component\Subsc

我正在努力将模型加载到组件中

我正在将条带订阅处理集成到Cakephp 3.3应用程序中,目前正在捕获和处理Webhook。我正在做的大部分工作都很好,但是当我尝试在SubscriptionComponent中加载“用户”和“计划”模型时遇到了麻烦

以下是我发送测试webhook时应用程序返回到Stripe的错误消息:

<response>
  <message>Call to undefined method App\Controller\Component\SubscriptionComponent::loadModel()</message>
  <url>/subscriptions/stripeHook</url>
  <code>500</code>
</response>
这是SubscriptionController的缩写版本:

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(也缩写为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,所以请对我耐心:-)我相信这很简单-只是对我来说还不明显


任何帮助都将不胜感激!Thx

尝试使用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();
        ...
    }   
}
namespace-App\Controller\Component;
...
使用Cake\ORM\TableRegistry;//Users=TableRegistry::get('Users');//Plans=TableRegistry::get('Plans');//用户->查找('all',['conditions'=>['User.stripe\u id'=>$customer]]);
$user=$user->first();
$plan=$this->Plans->find('all',['conditions'=>['plan.stripe\u id'=>$plan]]);
$plan=$plan->first();
...
}   
}

非常有魅力!谢谢!