我无法理解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链接到俱乐部
Typestypes.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:
ClubsUsers on the system are "Clubs". A club has various fields including a primary contact person - clubs.club_primary_contact_id
TeamsClubs 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
ContactsClubs can have one or more contacts associated with them. Linked to clubs via contacts.club_id
Typestypes.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/emailThe 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
更多推荐
发布评论