简体   繁体   中英

How to display data from different database table entities in one page in cakephp?

Suppose I have a mysql database and I have 3 entities,

  1. TV
  2. Laptop
  3. Accessories

Each entities contains specific information.
I want to print those data into a single ctp page. How can I do it?
I do not want to use foreign_key .

You can use Cake's loadModel function to call in data from any table into your current controller. More info towards the bottom of this page .

$this->loadModel('YourModel');

Then set the variable to make it available in your view:

$this->set('yourModel', $this->YourModel->find('all', $settings);
$this->loadModel('YourOtherModel');
$this->set('yourOtherModel', $this->YourOtherModel->findI('all', $settings);

Having now set two variables from your Model they are available to you in your view.

(Use debug to see all the data which is available ie debug($yourModel); You can put this in your controller action and it will output at the top of the page, or anywhere in your view file)

if you display data in table the code might be similler like this:

<table>
    <th>Title</th>
    <th>Title</th>        

        <?php foreach ($array as $temp_variable):?>
    <tr>
        <td><?php  echo $temp_variable['ModelName']['ColumnName']; ?></td>
        <td><?php  echo $temp_variable['ModelName']['ColumnName']; ?></td>
        <?php endforeach;?>
    </tr>
</table>

for one array..similar thing do for rest of the arrays... i hope you understand.

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