简体   繁体   中英

checking if table has entries with cakephp

I'm using cakephp and trying to check if a table has data entered in it. If there is no data, display a message saying "no data". If there is data, display it.

I can display the results fine, I'm just not sure how to tell cakephp to check if there is any info in the table.

Do I put the logic for checking in my model and in the view reference that model function? I'm new to cakephp and MVC in general, so I'm still trying to get a hang of the way data flows.

Edit- Here is my code for the index file. When I just hae $mysorts listed without a function I received the following error.

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in...

<h1>Sorted Entries</h1>
<?php
 echo $this->Html->link("Add List", array('action' => 'add')); 
   if (!empty($mysorts)) {
  ?>
<table>
   <tr>
       <th>ID</th>
       <th>Original</th>
       <th>Sorted</th>
   </tr>

  <?php  foreach ($mysorts as $mysort): ?>
         <tr>
             <td><?php echo $mysort['Mysort']['id']; ?></td>
             <td>
                 <?php echo $mysort['Mysort']['original']; ?>
             </td>
             <td> <?php echo $mysort['Mysort']['sorted']; ?>
             </td>
         </tr>
   <?php endforeach;
         } else {
        echo '<p>No results found!</p>';
        }
   ?>
</table>

And here is my controller

class MysortsController extends AppController {
  public $helpers = array('Html', 'Form');

  public function index() {
         $this ->set('mysorts', $this->Mysort->find('all'));  

  }

 public function add() {
        if($this->request->is('post')) {
        $this->Session->setFlash('yes');
        $this->redirect(array('action' => 'index'));
        }
 }

 function isempty(){
           $mysorts = $this->Mysort->find('all');
           $this->set('mysorts', $mysorts);
  }
}
?>

Assuming

$mysorts = $this->Mysort->find('all');
$this->set('mysorts', $mysorts);

In the controller, then you could check in the view:

if (!empty($mysorts) {
    // table and foreach loop
} else {
    echo '<p>No results found!</p>';
}

if you want to use any of the controller methods from view you can use requestAction

$result = $this->requestAction(array(
                                'controller' => 'mycontroller', 
                                'action' => 'action')
                 );   // $result will have return value from your action

echo $this->requestAction(array(
                                'controller' => 'mycontroller', 
                                'action' => 'action'),
                          array('return')
                 );   // will return rendered view

Request Action has always been a subject of debate that whether if it should be used. Some say using it with cache won't hurt the performance much, some say other wise. A while back I went through a post where it said that Request Action isn't as bad as everyone had anticipated.

Nevertheless, avoid using this. I would rather prefer an ajax call to the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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