问题
I want to be able to associate team members to a project using the ListBoxField
. I have a ProjectHolder
which has Project
pages as it's children. I also have a TeamHolder
which has TeamPage
's as its children.
I want to be able to save multiple team members in the ListBoxField
and then loop through them on a Project
page. I also want to be able to link to the team members pages. e.g
<% loop $TeamMemberNames %>
<a href="$Link">$Name</a>
<% end_loop %>
My Current code:
TeamPage.php
class TeamPage extends Page
{
private static $db = array(
'Name' => 'Varchar(255)',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
return $fields;
}
}
Project.php
class Project extends Page
{
private static $db = array(
'Name' => 'Varchar(255)',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPage',
'Select Team Members for project',
TeamPage::get()->map("ID", "Name")->toArray()
)->setMultiple(true));
return $fields;
}
}
Screenshot:
I can pull through the names from the TeamPage
Object into the ListBoxField
however after selecting the names I need a way to save the multiple values and also get the Link so I can link to the appropriate team members pages that are listed.
回答1:
To save data to the database we must first define a relationship to the data in our class. In this case we want to create a $many_many
relationship between Project
and TeamPage
.
In the Project
class we add a $many_many
relationship to TeamPage
. Here, the relationship name is TeamPages
.
When creating the ListBoxField
we pass the TeamPages
relationship name so the field knows where to save this data:
Project.php
class Project extends Page
{
private static $db = array(
'Name' => 'Varchar(255)'
);
private static $many_many = array(
'TeamPages' => 'TeamPage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPages',
'Select Team Members for project',
TeamPage::get()->map('ID', 'Name')->toArray()
)->setMultiple(true));
return $fields;
}
}
Now in our template we can loop through a Project's
$TeamPages
by calling the following:
Template
<% loop $TeamPages %>
<a href="$Link">$Name</a>
<% end_loop %>
If we would like a TeamPage
to be able to access it's related Projects
we can add a $belongs_many_many
to the TeamPage
class to point back to the Project
class. You can also add a ListBoxField
to control Projects
from the TeamPage
.
TeamPage.php
class TeamPage extends Page
{
private static $db = array(
'Name' => 'Varchar(255)'
);
private static $belongs_many_many = array(
'Projects' => 'Project'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'Projects',
'Select project for this team page',
Project::get()->map('ID', 'Name')->toArray()
)->setMultiple(true));
return $fields;
}
}
Something to note is TeamPage
and Project
both extend Page
. This means both classes inherit a Title
field. I would suggest using Title
instead of Name
, unless you have a specific reason to do so.
This would make the code:
Project.php
class Project extends Page
{
private static $many_many = array(
'TeamPages' => 'TeamPage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPages',
'Select Team Members for project',
TeamPage::get()->map('ID', 'Title')->toArray()
)->setMultiple(true));
return $fields;
}
}
TeamPage.php
class TeamPage extends Page
{
private static $belongs_many_many = array(
'Projects' => 'Project'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Projects', ListBoxField::create(
'Projects',
'Select project for this team page',
Project::get()->map('ID', 'Title')->toArray()
)->setMultiple(true));
return $fields;
}
}
Template
<% loop $TeamPages %>
<a href="$Link">$Title</a>
<% end_loop %>
来源:https://stackoverflow.com/questions/33647047/how-to-save-multiple-values-entered-into-listboxfield-and-loop-through-values