-3

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

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

谢谢你。

4

1 回答 1

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


    }
?>
于 2013-07-16T11:18:38.097 回答