【问题标题】:CakePHP 3 foreign key with matching table/model name具有匹配表/模型名称的 CakePHP 3 外键
【发布时间】:2015-05-22 22:18:18
【问题描述】:

CakePHP 3: 我正在尝试更新我的表中名为 poi 的外键。外部表名为 poi_types。不幸的是,DB-Designer 将外键命名为 poi_type 而不是 poi_type_id。 蛋糕回应

Passed variable is not an array or object, using empty array instead

我无法更改关系,因为正在运行的应用程序也会向此特定表发送请求。

任何想法,如何告诉 CakePHP 接受通过 POST 传递的值作为纯值?

编辑

视图包含以下字段:

<?= $this->Form->input('poi_type', ['options' => $poi_types_select]); ?>

$poi = $this->Pois->patchEntity($poi, $this->request->data);

它因上述错误而失败。删除输入字段时,一切都按预期工作。

我将外国 ID 更改为 poi_type_id,一切都像魅力一样。

是否有任何解决方法可以说服 Cake 更新此字段?

错误跟踪:

--------InvalidArgumentException------
⟩ ArrayObject->__construct
CORE/src/ORM/Marshaller.php, line 200
⟩ Cake\ORM\Marshaller->_prepareDataAndOptions
CORE/src/ORM/Marshaller.php, line 103
⟩ Cake\ORM\Marshaller->one
CORE/src/ORM/Marshaller.php, line 221
⟩ Cake\ORM\Marshaller->_marshalAssociation
CORE/src/ORM/Marshaller.php, line 558
⟩ Cake\ORM\Marshaller->_mergeAssociation
CORE/src/ORM/Marshaller.php, line 412
⟩ Cake\ORM\Marshaller->merge
CORE/src/ORM/Table.php, line 1978
⟩ Cake\ORM\Table->patchEntity
APP/Controller/PoisController.php, line 111
⟩ App\Controller\PoisController->add
[internal function]
⟩ call_user_func_array
CORE/src/Controller/Controller.php, line 410
⟩ Cake\Controller\Controller->invokeAction
CORE/src/Routing/Dispatcher.php, line 114
⟩ Cake\Routing\Dispatcher->_invoke
CORE/src/Routing/Dispatcher.php, line 87
⟩ Cake\Routing\Dispatcher->dispatch
ROOT/webroot/index.php, line 37

PoiTypes 上的表定义

class PoiTypesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        $this->table('poi_types');
        $this->hasMany('poi_type_translations');
        $this->belongsTo('poi',['foreignKey' => 'poi_type']);
    }

Poi 上的表定义

class PoisTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        $this->belongsTo('datasets');
        $this->hasMany('poi_translations',['foreignKey' => 'poi_id']);
        $this->hasOne('poi_types',['foreignKey' => 'id']);
    }

【问题讨论】:

  • 我不明白你的问题,整个描述很模糊。只需从请求中读取该字段作为任何其他字段? $this-&gt;request-&gt;data['Poi']['poi_type'] 也为您的问题添加一些代码。
  • 视图包含以下字段:&lt;?= $this-&gt;Form-&gt;input('poi_type', ['options' =&gt; $poi_types_select]); ?&gt; on $poi = $this-&gt;Pois-&gt;patchEntity($poi, $this-&gt;request-&gt;data); 它失败并出现上述错误。删除输入字段时,一切都按预期工作。
  • 您应该首先将其添加到您的问题中,并且还要添加确切的 CakePHP 版本。还要显示上述错误的全部痕迹,因为完全不清楚它是从哪里来的。
  • 对不起。我是新来写这样的问题。我按照建议更新了问题。谢谢!

标签: php mysql cakephp cakephp-3.0


【解决方案1】:

我认为发生错误是因为您的关联是这样设置的,Poi BelongsTo PoiType,我猜这会导致 Cake 在名称为“poi_type”的实体中构建关联的属性名称。

$poiEntity->poi_type // poi_type is expected to be an array or entity object

您需要更改实体中关联表的名称。 See this page of the manual.您需要更改属性名称。

$this->belongsTo('PoiTypes', [
    'className' => 'PoiTypes',
    'foreignKey' => 'poi_type',
    'propertyName' => 'poi_types'
]);

【讨论】:

  • 干得不错! @burzum 非常感谢。拯救了我的周末!下一杯啤酒在我身上
  • 欢迎您!并继续写详细的问题! ;-)
猜你喜欢
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 2021-06-17
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多