1

I have created a new controller to save subscription plans for my website. I am trying to create a new record through the model table but am getting the error: Call to a member function keys() on null in vendor/cakephp/cakephp/src/ORM/Table.php, line 2625.

The code I am trying to execute is.

namespace SubscriptionPlans\Model\Table;


use App\Model\Table\AppTable;
use Cake\Chronos\Chronos;
use Cake\Datasource\ConnectionInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\EntityInterface;
use SubscriptionPlans\Model\Entity\PartnerPlan;
use SubscriptionPlans\Model\Entity\PartnerPlanSubscription;
use SubscriptionPlans\Services\Period;

class PartnerPlanSubscriptionsTable extends AppTable
{
    /** @var ConnectionInterface  */
    public $_connection;

    public function __construct($arg)
    {
        parent::__construct($arg);
        $this->_connection = ConnectionManager::get('default');
    }

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created_at' => 'new',
                    'updated_at' => 'always',
                ]
            ]
        ]);
    }


    public function create($partnerId, $subscriptionPlan, $interval = 'year', $intervalCount = 1, $startDate = null): EntityInterface
    {
        // If the $subscriptionPlan is a PartnerPlan entity, grab the id.
        $planId = ($subscriptionPlan instanceof PartnerPlan) ? $subscriptionPlan->id : $subscriptionPlan;

        // Create a new date period based on the provided create params
        $period = new Period($interval, $intervalCount, $startDate);

        /** @var PartnerPlanSubscription $subscription */
        $subscription = $this->newEntity([
            'partner_id' => $partnerId,
            'partner_plan_id' => $planId,
            'starts_at' => $period->getStartDate()->toDateTimeString(),
            'ends_at' => $period->getEndDate()->toDateTimeString()
        ]);

        return $this->save($subscription);
    }
}

I can see the vendor/cakephp/cakephp/src/ORM/Table.php:2625 is expecting some kind of associated option, but I can't find anything in the documentation about this.

Ari Molzer
  • 11
  • 1
  • Is line 2625 in Table basically `$options['associated'] = $this->_associations->keys();` ? If so, somehow your Table hasn't been constructed properly, because that's when `_associations` is set. Look upstream to AppTable to make sure it's calling Table's Constructor. Or maybe something custom is clearing out _associations to null after construction? Bottom line this isn't a normal CakePHP lib issue it's something in your code that's messed with normal `_associations` values, it should always be an AssociationCollection – Andy Hoffner Nov 23 '20 at 23:09
  • Thanks I'll have a look :) Working on a new team project and not sure what exactly what I have inherited. @ahoffner – Ari Molzer Nov 23 '20 at 23:22

1 Answers1

0

This is an old question but this might be of help to others.

I ran into this same issue with PHP 8.0.13 and XDebug. This seems to be related to the XDebug incompatibility with __debugInfo() mentioned in https://github.com/cakephp/chronos/issues/164. I disabled the XDebug extension and the error magically went away.

Rolf Kaiser
  • 551
  • 6
  • 9