开发者

Custom table names in CakePHP

I am building a portfolio website using CakePHP and have named my model 'Portfolio' and my controller 'PortfolioControl开发者_如何转开发ler' however cake decides to look for a table called portfolios instead of just portfolio! How can I fix this as I don't want to call my table portfolios!

Also when dealing with the foreach loop in the views where it has statements like

<?php foreach ($posts as $post): ?> how would I deal with the plural issue with my portfolio?

Thanks

EDIT Here is the index.ctp file where I have the plural issue:

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

EDIT Here is the controller:

<?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';

  }

in you model you can set the $useTable variable to be whatever you need.

in views you pass variable values from controllers with set method

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

so the first parameter can be anything you like.


in the index action.

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

see the code here? that's what I meant.

you are setting $portfolio variable for your view. so you must change all your $posts variables into $portfolio variables.

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

or change portfolio in set method to posts like this.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜