繁体   English   中英

CakePHP中的自定义表名称

[英]Custom table names in CakePHP

我正在使用CakePHP建立一个投资组合网站,并命名了我的模型“ Portfolio”和我的控制器“ PortfolioController”,但是cake决定寻找一个称为投资组合的表,而不仅仅是投资组合! 我不想给我的桌子投资组合打电话,该如何解决呢!

同样,在视图中处理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'));
            }
        }
    }

}

?>
   Class Portfolio extends App_Model{

    public $useTable = 'portfolio';

  }

在模型中,您可以将$ useTable变量设置为所需的值。

在视图中,您可以使用set方法从控制器传递变量值

$this->set('portfolio',$array_of_data);

因此第一个参数可以是您喜欢的任何东西。

在索引动作中。

$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>

或将设置方法中的投资组合更改为此类。

$this->set('posts', $this->Portfolio->find('all'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM