请帮我解决CakePHP模型关系(Please help me with my CakePHP model relationships)

编程入门 行业动态 更新时间:2024-10-12 05:56:01
帮我解决CakePHP模型关系(Please help me with my CakePHP model relationships)

我无法理解CakePHP中的模型关联。 我之前没有使用OOP或MVC,我发现它有点棘手。

这是我有的:

Clubs

系统上的用户是“俱乐部”。 俱乐部有各种领域,包括主要联系人 - clubs.club_primary_contact_id

Teams

俱乐部可以有一个或多个与之相关的团队。 像俱乐部一样,球队也有一个主要联系人 - teams.team_primary_contact_id。 团队也有一个类型,并链接到类型表 - teams.type_id。 通过teams.club_id链接到俱乐部

Contacts

俱乐部可以有一个或多个与之关联的联系人。 通过contacts.club_id链接到俱乐部

Types

types.id和types.type_name

我希望能够访问club / view / x并显示显示以下内容的页面:

分会主要联系人姓名/电邮 所有俱乐部联系 所有俱乐部团队包括团队主要联系人姓名/电子邮

TeamsController的添加/编辑/删除功能正常工作 - 即我可以调用联系人和类型列表,并在视图表单的选择框中显示这些功能。

但是我无法从俱乐部模型中巧妙地访问所有这些。

我在这里的协会是什么? 这是我目前在模型课中的内容。

应用/型号/ Club.php

public $hasMany = array( 'Team' => array( 'className' => 'Team', 'foreignKey' => 'club_id' ), 'Contact' => array( 'className' => 'Contact', 'foreignKey' => 'club_id', 'order' => 'contact_name DESC' ) ); public $belongsTo = array ( 'ClubPrimaryContact' => array( 'className' => 'Contact' ) );

应用/型号/ Team.php

public $belongsTo = array ( 'TeamPrimaryContact' => array( 'className' => 'Contact' ), 'Club', 'Type' );

应用/型号/ Contact.php

public $hasManyAndBelongsTo = array( 'Team' => array( 'className' => 'Team' ) );

应用/型号/ Type.php

public $belongsTo = array ( 'Team' => array( 'className' => 'Team' ) );

I'm having trouble getting my head around my model associations in CakePHP. I've not worked with OOP or MVC before and I'm finding it a bit tricky.

Here is what I have:

Clubs

Users on the system are "Clubs". A club has various fields including a primary contact person - clubs.club_primary_contact_id

Teams

Clubs can have one or more teams associated with them. Like clubs, teams also have a primary contact person - teams.team_primary_contact_id. Teams also have a type and are linked to a types table - teams.type_id. Linked to clubs via teams.club_id

Contacts

Clubs can have one or more contacts associated with them. Linked to clubs via contacts.club_id

Types

types.id and types.type_name

I want to be able to access clubs/view/x and display a page showing the following:

Club primary contact name/email All club contacts All club teams including team primary contact name/email

The add/edit/delete functions for TeamsController works fine - i.e. I can call on the list of contacts & types and display those in a select box in the view form.

I'm having trouble accessing all of this neatly from the Club model though.

What are my associations here? Here is what I have in my model classes at present.

App/Model/Club.php

public $hasMany = array( 'Team' => array( 'className' => 'Team', 'foreignKey' => 'club_id' ), 'Contact' => array( 'className' => 'Contact', 'foreignKey' => 'club_id', 'order' => 'contact_name DESC' ) ); public $belongsTo = array ( 'ClubPrimaryContact' => array( 'className' => 'Contact' ) );

App/Model/Team.php

public $belongsTo = array ( 'TeamPrimaryContact' => array( 'className' => 'Contact' ), 'Club', 'Type' );

App/Model/Contact.php

public $hasManyAndBelongsTo = array( 'Team' => array( 'className' => 'Team' ) );

App/Model/Type.php

public $belongsTo = array ( 'Team' => array( 'className' => 'Team' ) );

最满意答案

坚持这些惯例将有助于此。

如果你想让俱乐部属于一个联系人 - 给它一个contact_id并在俱乐部模型中设置一个belongs_to关系给Contact。 您需要为Has_Many关系别名赋予该关系不同的别名 - 例如:

public $belongsTo = array( 'Leader' => array( 'className' => 'Contact' ) );

同样,对于团队,使用别名定义带有contact_id的belongs_to,并为联系人定义has_many关系。

然后在查询时 - 您需要将递归设置得足够高以获得所有结果,或者一旦完成,就可以使用可包含的行为优化查询。

然后按照正常查询并使用debug()查看返回的数组结构

Sticking to the conventions will help a bit here.

If you want the club to belong to a contact - give it a contact_id and set a belongs_to relation in the club model to Contact. You will need to give that relation a different Alias to the Has_Many relation alias - so something like:

public $belongsTo = array( 'Leader' => array( 'className' => 'Contact' ) );

Likewise for Teams, define a belongs_to with a contact_id for the leader with an alias, and a regular has_many relation for contacts.

Then when querying - you will need to set recursive high enough to get all the results, or once you have that working, refine your queries with containable behaviour.

Then just query as per normal & use debug() to see the array structure returned

更多推荐

本文发布于:2023-08-08 00:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帮我   模型   关系   relationships   CakePHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!