如何从Cake PHP中的两个不同表中获取数据 [英] How to get data from two different table in cake php

查看:142
本文介绍了如何从Cake PHP中的两个不同表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是它只会返回来自tab [1]的相同数据。.
i希望分别从tab [1]和tab [0]中获取数据

But it will give return same data from tab[1] only.. i want to get data from tab[1] and tab[0] distinctly

$ db = ConnectionManager :: getDataSource('default');

$db = ConnectionManager::getDataSource('default');

$ tab = $ db-> listSources();

$tab = $db->listSources();

        echo '<br>';
        $this->Form->useTable=$tab[1];
        print_r($this->Form->find(`all'));
        echo '<br>';
        $this->Form->use Table=$tab[0];
        print_r($this->Form->find('all'));


推荐答案

更改 Model-> useTable 在运行时无法正常工作,因为一旦模型初始化,CakePHP就会缓存数据库表的架构。

Changing Model->useTable at runtime does not work properly because once a model has been initialised, CakePHP caches the schema of the database-table.

要切换到另一个表并清除缓存的架构,使用 Model-> setSource('tablename')

To switch to another table and clear the cached schema, use Model->setSource('tablename')

Documentation; http://api.cakephp.org/2.3/source-class -Model.html#1100-1125

Documentation; http://api.cakephp.org/2.3/source-class-Model.html#1100-1125

您的示例将如下所示;

Your example will then look like this;

echo '<br>';
$this->Form->setSource($tab[1]);
print_r($this->Form->find(`all'));
echo '<br>';
$this->Form->setSource($tab[0]);
print_r($this->Form->find('all'));

另外,请使用 debug()代替 print_r()输出调试结果。这将输出正确格式化的结果。 (您需要在app / Config / core.php配置中将debug设置为1或更高,才能使debug()起作用)

Also, please use debug() to output results for debugging in stead of print_r(). This will output the results properly formatted. (You'll need to set debug to 1 or higher inside your app/Config/core.php configuration for debug() to work)

但是

切换模型的源表通常是不好的做法,并且仅适用于非常特殊的情况。我会强烈建议为每个数据库表创建一个单独的模型。

Switching the sourcetable of a Model is generally bad practice and will only apply to very specific cases. I would strongly suggest to create a separate model for each database table.

这篇关于如何从Cake PHP中的两个不同表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆