PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] loadModel() throws unexpected error in Cakephp 3

 March 09, 2022     cakephp, cakephp-3.0     No comments   

Issue

I am struggling with trying to load a model into a component.

I am integrating Stripe subscription-handling into a Cakephp 3.3 app, and am currently working on capturing and dealing with webhooks. Most of what I am doing is working well, however I run into trouble when I try to load the 'Users' and 'Plans' models in the SubscriptionComponent.

Here is the error message returned to Stripe by the app when I send a test webhook:

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

This is an abbreviated version of the 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;
}

This is the (also abbreviated) 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;
}   
}

I'm still learning CakePhp, so please be patient with me:-) I'm sure this is something simple - just not obvious to me yet.

Any help at all will be greatly appreciated! Thx


Solution

Try with 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();
        ...
    }   
}


Answered By - Jacek B Budzynski
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing