1
votes

CakePHP 3: I'm trying to update a foreign key in my table called poi. The foreign table is named poi_types. Unfortunately the DB-Designer named the foreign key poi_type instead of poi_type_id. Cake responds with

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

I'm not able to change the relation, because a working application also sends requests to this specific tables.

Any idea, how it is possible to tell CakePHP to accept the value passed via POST as a plain value?

EDIT

The View contains the following field:

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

on

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

it fails with the above mentioned error. When deleting the input-field everything works as expected.

I changed the foreign ID to poi_type_id and everything works like a charm as well.

Is there any workaround to convince Cake to update this field?

Error Trace:

--------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

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

TableDefinition on 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
I don't get your problem, the whole description is pretty vague. Just read the field from the request as any other field? $this->request->data['Poi']['poi_type'] Add some code to your question as well.floriank
The View contains the following field: <?= $this->Form->input('poi_type', ['options' => $poi_types_select]); ?> on $poi = $this->Pois->patchEntity($poi, $this->request->data); it fails with the above mentioned error. When deleting the input-field everything works as expected.Christian Gatzen
You should have added this to your question in the first place and also the exact CakePHP version. Show the whole trace of the above error as well because it is totally unclear from where this comes from.floriank
Sorry. I'm new to writing questions like that. I updated the Question as suggested. Thank you!Christian Gatzen

1 Answers

1
votes

I think the error happens because your association is set up like this, Poi BelongsTo PoiType, I guess this causes Cake to build the associated property name in the entity with a name of "poi_type".

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

You need to change the name of that associated table in the entity. See this page of the manual. You'll need to change the property name.

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