我有以下模特关系
Post belongsto PostCategory
PostCategory belongsto Forum
PostSubcat belongsto PostCategory
PostCategory hasmany Post
PostCategory hasmany PostSubcat
Forum hasmany PostCategory.
我正在使用我的ForumsController add()方法,我无法使用标准find('list')来显示用户可以添加帖子的所有论坛,类别和子类别。例如,下拉列表应该看起来像......
- Forum #1 Title
- Category #1.1 Title
- Subcat #1.1 Title
- Subcat #1.2 Title
- Category #1.2 Title
- Category #1.3 Title
- Subcat #2.1 Title
- Subcat #2.2 Title
- Forum #2 Title
- Category #2.1 Title
- Category #2.2 Title
所以我做了一个发现('all')就像这样...
$forums = $this->Forum->find('all', array(
'fields' => array('Forum.title'),
'conditions' => array('Forum.page_id' => $page_id),
'contain' => array(
'PostCategory' => array(
'fields' => array(
'PostCategory.id', 'PostCategory.title'
),
'PostSubcat' => array(
'fields' => array(
'PostSubcat.id', 'PostSubcat.title'
),
)
)
)
));
$this->set('forums', $forums);
我的add.ctp论坛有以下代码用于选择
<?php if($forums): ?>
<div class="input select required"><label for="PostCategoryId">Category</label>
<select name="data[Post][category_id]" id="PostCategoryId">
<option value="">Select a Category</option>
<?php foreach($forums as $forum): ?>
<?php if(!empty($forum['PostCategory'])): ?>
<option value="" class="SelectSeparator"><?php echo $forum['Forum']['title']; ?></option>
<?php foreach($forum['PostCategory'] as $category): ?>
<?php if($category_id == $category['id']){
$selected = "selected";
}else{
$selected = "not";
} ?>
<option value="<?php echo $category['id']; ?>" <?php echo $selected;?>><?php echo $category['title']; ?></option>
<?php foreach($category['PostSubcat'] as $subcat): ?>
<?php if($category_id == $subcat['id']){
$selected = "selected";
}else{
$selected = "not";
} ?>
<option value="<?php echo $subcat['id']; ?>"<?php echo $selected;?>>-- <?php echo $subcat['title']; ?></option>
<?php endforeach; // subcat?>
<?php endforeach; // category ?>
<?php endif; // if forum has categories ?>
<?php endforeach; // forum?>
</select>
</div>
<?php endif; // if there are forums ?>
这将显示我想要的选择选项并且它正确验证 - 如果用户选择论坛标题或选择“选择类别”而不是类别标题,则不会添加帖子。但是,它不是Cake友好的格式,因此不会显示验证错误。
有没有办法轻松将其转换为CakePHP Form-&gt;输入助手,以便验证错误消息能够正确显示?