【发布时间】:2021-03-06 17:02:27
【问题描述】:
我创建了一个新控制器来保存我的网站的订阅计划。我正在尝试通过模型表创建新记录,但出现错误:Call to a member function keys() on null in 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 选项,但我在文档中找不到任何关于此的内容。
【问题讨论】:
-
表中的第2625行基本上是
$options['associated'] = $this->_associations->keys();吗?如果是这样,不知何故您的表没有正确构建,因为那是设置_associations的时间。向上游查看 AppTable 以确保它正在调用 Table 的构造函数。或者,也许一些自定义的东西在构建后将 _associations 清除为空?底线这不是一个正常的 CakePHP 库问题,它是你的代码中的一些东西与正常的_associations值混淆,它应该始终是一个 AssociationCollection -
谢谢,我会看看 :) 处理一个新的团队项目,但不确定我到底继承了什么。 @ahoffner
标签: php cakephp cakephp-3.7