我正在使用CakePHP构建一个投资组合网站并命名我的模型'Portfolio'和我的控制器'PortfolioController'但是蛋糕决定寻找一个叫做投资组合的表而不仅仅是投资组合!我怎么能解决这个问题,因为我不想打电话给我的桌子组合!
当处理视图中的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'));
}
}
}
}
?>
答案 0 :(得分:11)
Class Portfolio extends App_Model{
public $useTable = 'portfolio';
}
在您的模型中,您可以将$ useTable变量设置为您需要的任何内容。
在视图中,您使用set方法
从控制器传递变量值$this->set('portfolio',$array_of_data);
所以第一个参数可以是你喜欢的任何东西。
答案 1 :(得分:2)
在索引动作中。
$this->set('portfolio', $this->Portfolio->find('all'));
在这里看到代码?这就是我的意思。
您为视图设置了$ portfolio变量。所以你必须将所有$ posts变量更改为$ portfolio变量。
<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 ($portfolio as $portfolio_item): ?>
<tr>
<td><?php echo $portfolio_item['Portfolio']['id']; ?></td>
<td>
<?php echo $this->Html->link($portfolio_item['Portfolio']['title'], array('action' => 'view', $portfolio_item['Portfolio']['id']));?>
</td>
<td>
<?php echo $this->Html->link(
'Delete',
array('action' => 'delete', $portfolio_item['Portfolio']['id']),
null,
'Are you sure?'
)?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $portfolio_item['Portfolio']['id']));?>
</td>
<td><?php echo $portfolio_item['Portfolio']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
或将set方法中的投资组合更改为此类帖子。
$this->set('posts', $this->Portfolio->find('all'));