如何在ZF表界面中进行连接查询?

发布于 2024-08-04 17:10:51 字数 345 浏览 7 评论 0原文

我的数据库和我的表格如下所示:

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 技术交流群。

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

发布评论

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

评论(1

四叶草在未来唯美盛开 2024-08-11 17:10:51
$models = new Models();
$select = $models->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
       ->join(array("a"=>"manufacturers"), 'models.manufacturer_id = a.id',
         array("man_name"=>"name", "man_description"=>"description"))
       ->where("a.name LIKE 'A%'");
$rowset = $models->fetchAll($select);

不幸的是,Zend_Db_Table 关系接口没有太多与从其声明的参考映射创建连接查询相关的智能。社区为复杂查询提供的解决方案是 Zend_Db_Table_Select 查询工厂。

请注意,您必须为制造商名称和描述提供列别名,否则这些列将在行数据的关联数组中隐藏模型名称和描述。您应该明确地命名列以避免这种情况。

但在你的例子中,我会跳过表接口和选择接口,而直接使用数据库适配器直接执行 SQL 查询:

$data = $db->fetchAll("
  SELECT m.*, a.name AS man_name, a.description AS man_description
  FROM Models m JOIN Manufacturers a ON m.manufacturer_id = a.id
  WHERE a.name LIKE 'A%'");

你将把数据作为一个简单的关联数组数组返回,而不是作为 Zend_Db_Table_Rowset。但由于连接的行集无论如何都是不可写的,因此您并没有做出太多牺牲。

$models = new Models();
$select = $models->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
       ->join(array("a"=>"manufacturers"), 'models.manufacturer_id = a.id',
         array("man_name"=>"name", "man_description"=>"description"))
       ->where("a.name LIKE 'A%'");
$rowset = $models->fetchAll($select);

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 the Zend_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:

$data = $db->fetchAll("
  SELECT m.*, a.name AS man_name, a.description AS man_description
  FROM Models m JOIN Manufacturers a ON m.manufacturer_id = a.id
  WHERE a.name LIKE 'A%'");

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.

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