提问者:小点点

具有匹配表/模型名称的CakePHP 3外键


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

表定义的PoiType

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']);
    }

共1个答案

匿名用户

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

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

您需要更改实体中关联表的名称。请参阅手册的这一页。您需要更改属性名称。

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