简体   繁体   English

检查表是否有cakephp条目

[英]checking if table has entries with cakephp

I'm using cakephp and trying to check if a table has data entered in it. 我正在使用cakephp,并尝试检查表是否在其中输入了数据。 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. 我可以很好地显示结果,但是我不确定如何告诉cakephp检查表中是否有任何信息。

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. 一般来说,我是Cakephp和MVC的新手,所以我仍在尝试掌握数据流的方式。

Edit- Here is my code for the index file. 编辑-这是我的索引文件代码。 When I just hae $mysorts listed without a function I received the following error. 当我只列出$ mysorts而没有函数时,收到以下错误。

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in... 解析错误:语法错误,意外的T_VARIABLE,期望T_FUNCTION在...

<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 如果要使用视图中的任何控制器方法,则可以使用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. 我希望对控制器进行ajax调用。

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

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