【发布时间】:2011-04-01 17:45:12
【问题描述】:
我正在使用 CakePHP 构建一个投资组合网站,并将我的模型命名为“投资组合”和我的控制器“投资组合控制器”,但是蛋糕决定寻找一个名为投资组合的表,而不仅仅是投资组合!由于我不想调用我的表投资组合,我该如何解决这个问题!
在处理视图中的 foreach 循环时,它有类似的语句
<?php foreach ($posts as $post): ?>我将如何处理我的投资组合的复数问题?
谢谢
编辑这是我遇到复数问题的 index.ctp 文件:
<p><?php echo $this->Html->link("Add Post", array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Action</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Portfolio']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Portfolio']['title'], array('action' => 'view', $post['Portfolio']['id']));?>
</td>
<td>
<?php echo $this->Html->link(
'Delete',
array('action' => 'delete', $post['Portfolio']['id']),
null,
'Are you sure?'
)?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Portfolio']['id']));?>
</td>
<td><?php echo $post['Portfolio']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
编辑这里是控制器:
<?php
class PortfolioController extends AppController
{
var $helpers = array ('Html','Form');
var $name = 'Portfolio';
function index()
{
$this->set('portfolio', $this->Portfolio->find('all'));
}
function view($id = null)
{
$this->Portfolio->id = $id;
$this->set('portfolio', $this->Portfolio->read());
}
function add()
{
if (!empty($this->data))
{
if ($this->Portfolio->save($this->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
function delete($id)
{
if ($this->Portfolio->delete($id))
{
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
function edit($id = null)
{
$this->Portfolio->id = $id;
if (empty($this->data))
{
$this->data = $this->Portfolio->read();
}
else
{
if ($this->Portfolio->save($this->data))
{
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
【问题讨论】:
-
打电话给你的桌子
portfolio有点用词不当。它可能应该是works,因为每个单独的项目都代表您作品集中的一件作品。或portfolios,如果表中的每个项目都包含一个投资组合。它更合乎逻辑,更适合 Cake 约定。 Cake 喜欢某些事物的复数名称是有原因的,因为从长远来看,它更符合逻辑和一致性。