从主页上的数据库中检索数据

时间:2013-07-16 10:51:14

标签: mysql database cakephp frameworks

我是使用CakePhp框架进行编程的初学者,我想在网站的HOME页面上显示数据库中的数据。

我已经创建了模型+视图+控制器。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在主页控制器中执行此操作

<?php
    class HompageController extends AppController {


    public function index(){
      ...

      //For us to be able to call a different Model if it doesnt have a relationship with this model. Instantiate Product Model
      $Products = ClassRegistry::init('Product');

      //to get the data in product database. you can do it in two ways
      //1. use the find method to get all data in Product database
      $myproducts = $Products->find('all');

      //2. or call some function in your Product model e.g. get_products() that return the data.
      $myproducts = $Products->get_products();


      //pass myproducts variable to homepage index
      $this->set('myproducts', $myproducts);


    }
?>