php - CakePHP 3 foreign key with matching table/model name

339

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

Answer

Solution:

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

People are also looking for solutions to the problem: apache - Disable gzip compression on Openshift (php cartridge)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.