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

标签 php mysql cakephp cakephp-3.0

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 传递的值作为普通值吗?

编辑

View 包含以下字段:

<?= $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 上的 TableDefinitions

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

最佳答案

我认为错误的发生是因为你的关联是这样设置的,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'
]);

关于php - 具有匹配表/模型名称的 CakePHP 3 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397446/

相关文章:

javascript - 如何从jQuery数据库中获取星级评分数据?

php - SQL查询从两个表中抓取数据

mysql - cakephp 3(My)SQL聚合函数max()语法错误接近 'AS MAX(`用户`_ _`date_time`)AS `date`

php - 了解 Twitter API 的 "Cursor"参数

字符串结尾可以包含随机数时的PHP字符串比较

mysql - 在Rails vs Java中执行原始SQL

javascript - Post方法后重定向

php - 用户名、密码、加盐、加密、哈希——这一切是如何运作的?

cakephp - CAKE PHP 更改日期格式

jquery - CakePHP 1.3 : How can I check how many visitors I have in my site at a specific time?