【问题标题】:Custom table names in CakePHPCakePHP 中的自定义表名
【发布时间】: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 喜欢某些事物的复数名称是有原因的,因为从长远来看,它更符合逻辑和一致性。

标签: php cakephp


【解决方案1】:
   Class Portfolio extends App_Model{

    public $useTable = 'portfolio';

  }

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

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

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

所以第一个参数可以是任何你喜欢的。

【讨论】:

  • 进入你的模型;这是模型的属性
  • 很酷的工作:) 第二个问题呢,因为目前我收到未定义的变量错误,因为这里的帖子和帖子复数的事情:driz.co.uk/cake/portfolio
  • 根据 CakePHP Book 上的教程(用于博客),我拥有所有正确的代码...我不确定该集合的用途和放置位置?
  • 如果你在 $post vars 中引用 $post['Portfolio'] 模型名称,在你的模型中设置 $useTable 后它应该是完全可用的。
  • 正如我所说,您的 $posts 变量未设置。请提供您的代码表单投资组合控制器,索引操作。我们会看看有什么问题。
【解决方案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'));

【讨论】:

  • 假设我将其保留为投资组合,我将如何处理 foreach 循环?肯定会开始将投资组合作为投资组合吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多