CakePHP 3保存属于多个协会未知类型&“&"错误 [英] CakePHP 3 Saving BelongsToMany Association Unknown type "" Error

查看:64
本文介绍了CakePHP 3保存属于多个协会未知类型&“&"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直把头撞在墙上,试图弄清为什么我的电子邮件(属于ToMany来宾(属于ToMany电子邮件))无法保存.当我尝试使用相关数据(来宾)保存电子邮件模型时,它在$ this-> Emails-> save($ email)中失败,并带有:

I've been banging my head against the wall trying to figure out why my Emails (belongsToMany Guests (belongsToMany Emails)) won't save. When I attempt to save the email model with associated data (guests) it fails at $this->Emails->save($email) with:

未知类型"错误 InvalidArgumentException

Unknown type "" Error InvalidArgumentException

我遵循了CakeBookmarks示例( http:此时将T恤//book.cakephp.org/3.0/zh-CN/tutorials-and-examples/bookmarks/intro.html )发送给发球台,以查找关系和编辑表单.甚至构建它并从中提取代码.

I've followed the CakeBookmarks example (http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html) to the tee at this point for relationships and the edit form. And even built it and pulled code from it.

这是我项目中的相关代码,感谢您的帮助或推测:

Here's the relevant code from my project, any help or speculation is appreciated:

为emails_guests连接表创建语句:

Create statement for the emails_guests join table:

CREATE TABLE emails_guests (
    email_id INT NOT NULL,
    guest_id INT NOT NULL,
    PRIMARY KEY (email_id, guest_id),
    FOREIGN KEY guest_key(guest_id) REFERENCES guests(id),
    FOREIGN KEY email_key(email_id) REFERENCES emails(id)
);

EmailsTable :: initialize

EmailsTable::initialize

public function initialize(array $config)
{
    // parent::initialize($config);

    $this->table('emails');
    $this->displayField('title');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Guests', [
        'foreignKey' => 'email_id',
        'targetForeignKey' => 'guest_id',
        'joinTable' => 'emails_guests'
    ]);
}

GuestsTable :: intialize

GuestsTable::intialize

public function initialize(array $config){
    parent::initialize($config);

    $this->table('guests');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Invitations', [
        'foreignKey' => 'invitation_id'
    ]);
    $this->hasMany('Messages', [
        'foreignKey' => 'guest_id'
    ]);
    $this->belongsTo('SeatingGroups', [
        'foreignKey' => 'seating_group_id'
    ]);
    $this->belongsToMany('Emails', [
        'foreignKey' => 'guest_id',
        'targetForeignKey' => 'email_id',
        'joinTable' => 'emails_guests'
    ]);
}

电子邮件创建/更新视图manage.ctp

Emails create/update view manage.ctp

            <?= $this->Form->create($email); ?>
                <div class="row">
                    <div class="col-sm-12">
                        <?= $this->Form->input('title', ['id' => false]); ?>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <?= $this->Form->input('subject'); ?>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <?= $this->Form->input('message', ['class'=>'email-body']); ?>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <?= $this->Form->input(
                            'guests._ids',
                            [   
                                // 'type' => 'select',
                                'options' => $guests,
                                // 'multiple' => true,
                                // 'empty' => 'Please select the recipients.',
                                // 'label'=>'Recipients'
                            ]
                        ); ?>
                    </div>
                    <div class="col-sm-6">
                        <div class="row">
                            <div class="col-sm-12">
                                <?= $this->Form->input('send_by', ['id' => 'send-by', 'type' => 'text']); ?>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $this->Form->checkbox('now'); ?>
                                <label for="now">Send Now</label>
                            </div>
                            <div class="col-sm-6">
                                <?= $this->Form->input(
                                    __('Save'),
                                    [
                                        'type' => 'submit',
                                        'class' => 'inline',
                                        'label' => false
                                    ]
                                ) ?>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                    </div>
                </div>
                <div class="clearfix"></div>
            <?= $this->Form->end() ?>

最后,控制器处理有问题的保存:

And lastly the controller handling the save in question:

public function manage($id = null) {
    $this->set('title', 'Manage Emails');

    if(isset($id)) {
        $email = $this->Emails->get($id, [
            'contain' => ['Guests']
        ]);
        $this->set('title', 'Email: ' . $email->title);
    } else {
        $email = $this->Emails->newEntity();
        $this->set('title', 'Create Email');
    }
    if ($this->request->is(['patch', 'post', 'put'])) {
        debug($email); 
        $email = $this->Emails->patchEntity($email, $this->request->data);
        debug($email); 
        debug($this->request->data);
        if ($this->Emails->save($email)) {

            if($email->now == true) {
                $this->process($email->id, $id);
            }
            $this->Flash->success('The email has been saved.');
            return $this->redirect(['action' => 'manage', $email->id]);
        } else {
            $this->Flash->error('The email could not be saved. Please, try again.');
        }
    }
    $guests = $this->Emails->Guests->find('list')->select(['id', 'first_name', 'last_name', 'email'])->toArray();

    $this->set('email', $email);
    $this->set('guests', $guests);
    $emails = $this->Emails->find();
    $this->set('emails', $this->paginate($emails));
    $this->set('_serialize', ['emails']);
}

当我保存一封电子邮件并选择一个或多个来宾时的请求数据(触发无效的类型"错误):

The request data when I save an email and select one or more guests (which triggers the invalid type "" error):

[
    'title' => 'Test Email Edit',
    'subject' => 'Hi! {{guest.first_name}}, we're testing some new features!2',
    'message' => 'Hi {{guest.first_name}} {{guest.last_name}},
We're testing some new features with recursive data. Let me tell you a bit about you:

Your seating group is: {{guest.seating_group.name}}.
Your invitation is: {{guest.invitation.name}}
Which we sent to: {{guest.invitation.address}}

Thanks! Hope this comes out okay... 
',
    'guests' => [
        '_ids' => [
            (int) 0 => '1'
        ]
    ],
    'send_by' => '',
    'now' => '0'
]

堆栈跟踪:

⟩ Cake\Database\Type::build
CORE/src/ORM/Table.php, line 1552
⟩ Cake\ORM\Table->_newId
CORE/src/ORM/Table.php, line 1489
⟩ Cake\ORM\Table->_insert
CORE/src/ORM/Table.php, line 1436
⟩ Cake\ORM\Table->_processSave
CORE/src/ORM/Table.php, line 1367
⟩ Cake\ORM\Table->Cake\ORM\{closure}
CORE/src/Database/Connection.php, line 561
⟩ Cake\Database\Connection->transactional
CORE/src/ORM/Table.php, line 1368
⟩ Cake\ORM\Table->save
CORE/src/ORM/Association/BelongsToMany.php, line 557
⟩ Cake\ORM\Association\BelongsToMany->_saveLinks
CORE/src/ORM/Association/BelongsToMany.php, line 506
⟩ Cake\ORM\Association\BelongsToMany->_saveTarget
CORE/src/ORM/Association/BelongsToMany.php, line 750
⟩ Cake\ORM\Association\BelongsToMany->Cake\ORM\Association\{closure}
CORE/src/Database/Connection.php, line 561
⟩ Cake\Database\Connection->transactional
CORE/src/ORM/Association/BelongsToMany.php, line 769
⟩ Cake\ORM\Association\BelongsToMany->replaceLinks
CORE/src/ORM/Association/BelongsToMany.php, line 445
⟩ Cake\ORM\Association\BelongsToMany->saveAssociated
CORE/src/ORM/AssociationCollection.php, line 254
⟩ Cake\ORM\AssociationCollection->_save
CORE/src/ORM/AssociationCollection.php, line 230
⟩ Cake\ORM\AssociationCollection->_saveAssociations
CORE/src/ORM/AssociationCollection.php, line 195
⟩ Cake\ORM\AssociationCollection->saveChildren
CORE/src/ORM/Table.php, line 1447
⟩ Cake\ORM\Table->_processSave
CORE/src/ORM/Table.php, line 1367
⟩ Cake\ORM\Table->Cake\ORM\{closure}
CORE/src/Database/Connection.php, line 561
⟩ Cake\Database\Connection->transactional
CORE/src/ORM/Table.php, line 1368
⟩ Cake\ORM\Table->save
APP/Controller/EmailsController.php, line 61
⟩ App\Controller\EmailsController->manage
[internal function]
⟩ call_user_func_array
CORE/src/Controller/Controller.php, line 411
⟩ 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

根据文档和示例,它应该可以正常工作.预先感谢!

It should work fine according to the docs and examples. Thanks in advance!

推荐答案

读/缓存架构似乎有问题,或者您为联接表类定义了错误的主键.使用复合主键(如您在此处显示的主键),它永远都不会到达代码中会抛出该异常的地步.

Looks like something is wrong with either the read/cached schema, or you are defining an incorrect primary key for the join table class. With a compound primary key like the one you are showing here, it should never reach the point in code where that excpetion is thrown.

删除tmp/cache/models/,如果您有EmailsGuestsTable类,请检查是否通过Table::primaryKey()设置了它,如果使用了它,则应设置它

Delete tmp/cache/models/, and in case you have a EmailsGuestsTable class, check if and what it sets via Table::primaryKey(), in case used, it should be

['email_id', 'guest_id']

也许您的表最初具有不同的主键,然后您对其进行了更改,而无需清除缓存和/或更新相应的表类.

Maybe your table initially had a different primary key, and you've made changes to it afterwards without clearing the cache and/or updating the corresponding table class.

这篇关于CakePHP 3保存属于多个协会未知类型&amp;“&amp;"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆