从两个表中查找结果时选择字段名称
我有两个表(恰好位于两个不同的数据库中)。 “客户端”和“域”,客户端可以有多个域。
这是我正在使用的代码:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果客户端
hasMany
域,那么您的模型应该像$this->Domain->find('all');
那样显式传递字段Client. id
将显示错误,因为它不是域表的一部分,使用debug=2
启用 sql 转储并查看查询是如何运行的。你的模型应该是
这样的
If clients
hasMany
domains, So your models should be called like$this->Domain->find('all');
explicitly passing fieldsClient.id
will show error, as its not the part of domains table, enable sql dumping usingdebug=2
and see how queries are being run.You models should be
This should work like this
如果您的两个表位于不同的数据库中,那么您的生活就会变得很困难。 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