php - 在 vendor/cakephp/cakephp/src/ORM/Table.php 中调用成员函数 keys(),第 2625 行
问题描述
我创建了一个新控制器来保存我的网站的订阅计划。我正在尝试通过模型表创建新记录,但出现错误:Call to a member function keys() on null
在vendor/cakephp/cakephp/src/ORM/Table.php, line 2625
.
我要执行的代码是。
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);
}
}
我可以看到vendor/cakephp/cakephp/src/ORM/Table.php:2625
期待某种associated
选项,但我在文档中找不到任何关于此的内容。
解决方案
这是一个老问题,但这可能对其他人有帮助。
我在 PHP 8.0.13 和 XDebug 中遇到了同样的问题。这似乎与https://github.com/cakephp/chronos/issues/164__debugInfo()
中提到的 XDebug 不兼容有关。我禁用了 XDebug 扩展,错误神奇地消失了。
推荐阅读
- python - 如何覆盖调用脚本中的变量?
- html - 如何更改引导程序 4 轮播中的导航按钮?
- c# - 在绑定列上设置元素集合的 DataGridViewComboBoxColumn 不允许用户下拉
- vuejs2 - 如何在 vue2-daterange-picker 中实现“清除”按钮?
- azure-active-directory - Azure AD Enterprise 应用程序角色更改不会触发 Zoom 或 DocuSign 的预配更新
- vue.js - 如何在 CSS 背景中使用 BASE_URL 等 vue.js 环境变量:url(...)
- javascript - 在 Table Javascript 中添加行数
- github - 如何在詹金斯的任意网址部署 git hub 拉取请求?
- python - TensorFlow / Keras Python CNN
- r - 将一个组与 R 中的其他组作为一个整体进行比较