简体   繁体   English

CakePHP同时更新两个表

[英]CakePHP update two table in same time

Hi I'm working on a project for a nightclub system. 嗨,我正在为一个夜总会系统项目。 Our client wants to have an electronic sign in page at events, where he can sign in patrons automatically into the database. 我们的客户希望在活动中拥有一个电子登录页面,在那里他可以自动将顾客登录到数据库中。 At the same time, he wants to be able to identify which patrons attended which events. 同时,他希望能够确定哪些顾客参加了哪些活动。 In our database we've accommodated this by using a "Guestlist" table that joins an event ID (event) with a patron ID. 在我们的数据库中,我们通过使用“来宾列表”表来容纳这一点,该表将事件ID(事件)与顾客ID结合在一起。

My interpretations of event-flow at this moment is: 目前,我对事件流的解释是:

  • A patron comes to the counter and gives info to be enterred into database. 一位顾客来到柜台,并将信息输入数据库。

  • Info is submitted and goes into the Patron Table, generating a patron ID. 信息被提交并进入顾客表,生成顾客ID。

  • The Guest list table for said event then gets the patron ID and the event ID of the event, both referenced from respective tables, and joins the tables. 然后,所述事件的来宾列表表获得从各自的表中引用的事件的顾客ID和事件ID,并加入表。

  • from the guest list table the user can view the events ID and patron info that was submitted. 从来宾列表中,用户可以查看事件ID和已提交的顾客信息。

I'm not sure whether I've missed a step or if our database is incomplete/incorrect but we have no idea how to procceed with this in cakePHP. 我不确定是否错过了一步,或者我们的数据库是否不完整/不正确,但是我们不知道如何在cakePHP中进行此操作。

If the tables are related and your models are set up correctly you can use the saveAssociated 如果表相关并且您的模型设置正确,则可以使用saveAssociated

Your models should be set up with the relevant relationships. 您的模型应设置有相关关系。 Normally it would be a hasAndBelongsToMany relationship, but as you want to use the GuestList data for checkin a hasManyThrough would be better 通常,这将是hasAndBelongsToMany关系,但是由于您希望使用GuestList数据进行签入, hasManyThrough会更好

This is very roughed out to give a starting point, please.. please read the docs I've linked to for a more complete example of the solution 这是一个非常粗糙的步骤,请给出一个起点。.请阅读我链接到的文档,以获取解决方案的更完整示例

<?php
class Patron extends AppModel
{
    public $hasMany = array('GuestList');
}

class GuestList extends AppModel 
{
    public $belongsTo = array(
        'Patron',
        'Event'
    );
}

class Event extends AppModel
{
    public $hasMany = array('GuestList')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM