如何在ZF表界面中进行连接查询?
我的数据库和我的表格如下所示:
alt text http://img15.imageshack .us/img15/2568/stackdijag.png
我想要做的是获取制造商名称列以 A 开头的所有型号。 这意味着查询的简单部分应该类似于 $manufacturers->fetchAll("name LIKE '$letter%'");
我正在尝试通过采埃孚关系来实现这一目标,但它不会成功,所以欢迎任何形式的帮助......
I have the db and my tables look like this:
alt text http://img15.imageshack.us/img15/2568/stackdijag.png
What I want to do is to get all models where manufacturers name column starts with A.
Which means that that simple part of query should be like $manufacturers->fetchAll("name LIKE '$letter%'");
I am trying to accomplish this with ZF relations but it ain't going, so any kind of help is welcome...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,
Zend_Db_Table
关系接口没有太多与从其声明的参考映射创建连接查询相关的智能。社区为复杂查询提供的解决方案是Zend_Db_Table_Select
查询工厂。请注意,您必须为制造商名称和描述提供列别名,否则这些列将在行数据的关联数组中隐藏模型名称和描述。您应该明确地命名列以避免这种情况。
但在你的例子中,我会跳过表接口和选择接口,而直接使用数据库适配器直接执行 SQL 查询:
你将把数据作为一个简单的关联数组数组返回,而不是作为
Zend_Db_Table_Rowset
。但由于连接的行集无论如何都是不可写的,因此您并没有做出太多牺牲。Unfortunately the
Zend_Db_Table
relationships interface doesn't have much intelligence in it related to creating joined queries from its declared reference map. The community-contributed solution for complex queries is theZend_Db_Table_Select
query factory.Note you have to give column aliases for manufacturer's name and description, or else these columns will suppress the model's name and description in the associative array for the row data. You should name columns distinctly to avoid this.
But in your case, I'd skip the table interface and the select interface, and simply execute an SQL query directly using the Db adapter:
You'll get the data back as a simple array of associative arrays, not as a
Zend_Db_Table_Rowset
. But since a joined rowset isn't writeable anyway, you haven't sacrificed much.