我正在建設一個使用CakePHP的投資組合網站,並命名了我的模型「投資組合」和我的控制器'PortfolioController',但蛋糕決定尋找名爲投資組合的表格而不僅僅是投資組合!我怎麼能解決這個問題,因爲我不想打電話給我的表格組合!在該意見foreach循環它有一個像CakePHP中的自定義表名
<?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'));
}
}
}
}
?>
調用你的表「組合」是一種誤稱。它應該是'作品',因爲每個單獨的項目都代表了您投資組合中的一件作品。或者「投資組合」,如果表中的每個項目都包含投資組合。這更符合邏輯,並與蛋糕公約更好。有一個原因是爲什麼Cake在某些事情上喜歡複數名稱,因爲從長遠來看,它更符合邏輯和一致性。 – deceze 2011-04-03 02:24:50