dsf4354353452 2011-01-31 08:46
浏览 36
已采纳

CakePHP:在类似MVC的结构中使用SQL查询语句的方法是什么

I wonder how do we use this in an MVC framework, CakePHP in particular.

$query = oci_parse($c, "SELECT * FROM JOB ORDER BY job_title");
                        oci_execute($query);

                        while($row = oci_fetch_assoc($query)){
                            $showRowJ .= "<tr>
";
                            $showRowJ .= "<td><a href = 'job_delete.php?jobid=".$row['JOB_ID']."'> Delete </a></td>
";
                            $showRowJ .= "<td><a href = 'job_update.php?jobid=".$row['JOB_ID']."'> ".$row['JOB_ID']." </a></td>
";
                            $showRowJ .= "<td>".$row['JOB_TITLE']."</td>
";
                            $showRowJ .= "<td>".$row['REQUEST_DATE']."</td>
";
                            $showRowJ .= "<td>".$row['START_DATE']."</td>
";
                            $showRowJ .= "<td>".$row['NUMBER_OF_DAYS']."</td>
";
                            $showRowJ .= "<td>".$row['STATUS']."</td>
";
                            $showRowJ .= "<td>".$row['CLIENT_ID']."</td>
";
                            $showRowJ .= "</tr>
";
                            }

and if for MySQL: (example)

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

I'm confused some say it should be in a model and some are using find('all')

UPDATE:

Hey sorry I meant when we want to query joint tables. The above is just an example when we need to include WHERE 2-3 times for joining tables.

Let's say I want to query:

SELECT users.name, foods.name FROM users, foods WHERE users.id='1' AND users.id=foods.id 

Something like above.. How should we do it in CakePHP ?

Thanks.

  • 写回答

3条回答 默认 最新

  • dqrmkdu25623 2011-01-31 08:53
    关注

    Since Cake includes a complete database abstraction layer, you wouldn't write any sort of SQL at all. You'd set up your models correctly and then to something along these lines:

    Controller

    $jobs = $this->Job->find('all', array('order' => array('Job.title' => 'asc')));
    $this->set(compact('jobs'));
    

    View

    <table>
    <?php foreach ($jobs as $job) : ?>
        <tr><?php echo htmlentities($job['Job']['title']); ?></tr>
        ...
    <?php endforeach; ?>
    </table>
    

    Maybe you should follow the tutorial. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合