从两个表中查找结果时选择字段名称

发布于 2024-10-06 08:07:04 字数 504 浏览 3 评论 0原文

我有两个表(恰好位于两个不同的数据库中)。 “客户端”和“域”,客户端可以有多个域。

这是我正在使用的代码:

$this->Domain->find('all', array(
    'order' => 'domain ASC',
    'fields' => array(
        'Domain.id',
        'Domain.domain',
        'Server.name',
        'Client.id',
        'Client.name'
    )
));

当我不使用 'fields' => 返回所有字段时array() 一切正常,当我询问特定字段时,它说:

SQL 错误:1054:未知列 “字段列表”中的“Client.id”

如果我只删除两个客户端列(客户端模型是另一个数据库上的唯一模型),一切都会正常工作。

I have two tables (which happen to be in two different databases). "clients" and "domains", clients can have multiple domains.

This is the code i am using:

$this->Domain->find('all', array(
    'order' => 'domain ASC',
    'fields' => array(
        'Domain.id',
        'Domain.domain',
        'Server.name',
        'Client.id',
        'Client.name'
    )
));

When i return all the fields by not using the 'fields' => array() everything works fine, as soon as i ask for specific fields, it says:

SQL Error: 1054: Unknown column
'Client.id' in 'field list'

Everything also works fine if i just remove the two Client columns (The Client model is the only model which is on another database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-10-13 08:07:04

如果客户端 hasMany 域,那么您的模型应该像 $this->Domain->find('all'); 那样显式传递字段 Client. id 将显示错误,因为它不是域表的一部分,使用 debug=2 启用 sql 转储并查看查询是如何运行的。

你的模型应该是

// in client.php model - having structure - id, name
$hasMany = 'Domain';

// in domain.php model - having structure - id, name, client_id
$belongsTo = 'Client';

这样的

$this->Domain->recursive = 1;
$data = $this->Domain->find('all');
// $data = Array ( 'Domain' => ********, 'Client' => ****** )

If clients hasMany domains, So your models should be called like $this->Domain->find('all'); explicitly passing fields Client.id will show error, as its not the part of domains table, enable sql dumping using debug=2 and see how queries are being run.

You models should be

// in client.php model - having structure - id, name
$hasMany = 'Domain';

// in domain.php model - having structure - id, name, client_id
$belongsTo = 'Client';

This should work like this

$this->Domain->recursive = 1;
$data = $this->Domain->find('all');
// $data = Array ( 'Domain' => ********, 'Client' => ****** )
☆獨立☆ 2024-10-13 08:07:04

如果您的两个表位于不同的数据库中,那么您的生活就会变得很困难。 AFAIK,Cake 不支持在两个不同数据库之间连接两个表(或强制关系)。为什么将客户端表放在单独的数据库中?

如果您无法移动表,我认为您必须在域模型中编写一些自定义代码,以便它将使用域的(默认)数据库连接字符串,但会实例化并连接另一个数据库资源到另一个数据库。请参阅 http://bakery.cakephp.org/articles/doze/2010/03/12/use-multiple-databases-in-one-app-based-on-requested-url 了解如何操作-- 向前跳到标题为“动态选择正确的数据库”的部分。

哈特哈,
特拉维斯

If your two tables are in different databases, you're really making your life difficult. AFAIK, Cake doesn't support joining two tables (or enforcing relationships) between two different databases. Why do you have the client table in a separate db?

If you can't move your table, I think you're going to have to write some custom code inside your Domain model, so that it will use the (default) db connection string for domains, but will instantiate and connect another db resource to the other database. See http://bakery.cakephp.org/articles/doze/2010/03/12/use-multiple-databases-in-one-app-based-on-requested-url for how to do it -- skip ahead to the section entitled, "Select Correct Database Dynamically".

HTH,
Travis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文