自动连接表而不破坏 Zend Framework 中的默认行为 [英] Automatically joining tables without breaking default behaviour in Zend Framework

查看:17
本文介绍了自动连接表而不破坏 Zend Framework 中的默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:我有 2 个模型:'Action' 和 'User'.这些模型分别引用表actions"和users".

The situation is as follows: I've got 2 models: 'Action' and 'User'. These models refer to the tables 'actions' and 'users', respectively.

我的操作表包含一列 user_id.此时,我需要对所有操作以及它们分配给的用户的概览.当我使用 $action->fetchAll() 时,我只有用户 ID,所以我希望能够加入来自用户模型的数据,最好不要调用 findDependentRowset().

My action table contains a column user_id. At this moment, I need an overview of all actions, and the users to which they are assigned to. When i use $action->fetchAll(), I only have the user ID, so I want to be able to join the data from the user model, preferably without making a call to findDependentRowset().

我想在我的模型中创建自定义的 fetchAll()fetchRow()find() 方法,但这会破坏默认值行为.

I thought about creating custom fetchAll(), fetchRow() and find() methods in my model, but this would break default behaviour.

解决此问题的最佳方法是什么?任何帮助将不胜感激.

What is the best way to solve this issue? Any help would be greatly appreciated.

推荐答案

我在 Zend Framework 中设计并实现了表关系功能.

I designed and implemented the table-relationships feature in Zend Framework.

我的第一条评论是无论如何您都不会使用 findDependentRowset() -- 如果 Action 具有对 User 的外键引用,您将使用 findParentRow().

My first comment is that you wouldn't use findDependentRowset() anyway -- you'd use findParentRow() if the Action has a foreign key reference to User.

$actionTable = new Action();
$actionRowset = $actionTable->fetchAll();
foreach ($actionRowset as $actionRow) {
  $userRow = $actionRow->findParentRow('User');
}

在循环中,您现在有一个 $actionRow 和一个 $userRow 对象.您可以通过更改对象字段并在对象上调用 save() 通过任一对象将更改写回数据库.

In the loop, you now have an $actionRow and a $userRow object. You can write changes back to the database through either object by changing object fields and calling save() on the object.

您还可以使用 Zend_Db_Table_Select 类(在我离开项目后实现)基于 Action 和 User 之间的连接检索行集.

You can also use the Zend_Db_Table_Select class (which was implemented after I left the project) to retrieve a Rowset based on a join between Action and User.

$actionTable = new Action();
$actionQuery = $actionTable->select()
  ->setIntegrityCheck(false) // allows joins
  ->from($actionTable)
  ->join('user', 'user.id = action.user_id');
$joinedRowset = $actionTable->fetchAll($actionQuery);
foreach ($joinedRowset as $joinedRow) {
  print_r($joinedRow->toArray());
}

请注意,这种基于连接查询的行集是只读的.您不能在 Row 对象中设置字段值并调用 save() 将更改发布回数据库.

Note that such a Rowset based on a join query is read-only. You cannot set field values in the Row objects and call save() to post changes back to the database.

无法使任意连接的结果集可写.考虑一个基于上述连接结果集的简单示例:

There is no way to make an arbitrary joined result set writable. Consider a simple example based on the joined result set above:

action_id  action_type  user_id  user_name
   1          Buy          1       Bill
   2          Sell         1       Bill
   3          Buy          2       Aron
   4          Sell         2       Aron

接下来对于 action_id=1 的行,我更改来自 User 对象的字段之一:

Next for the row with action_id=1, I change one of the fields that came from the User object:

$joinedRow->user_name = 'William';
$joinedRow->save();

问题:当我查看 action_id=2 的下一行时,我应该看到Bill"还是William"?如果是威廉",这是否意味着保存第 1 行必须在此结果集中的所有其他行中自动将比尔"更新为威廉"?或者这是否意味着 save() 会自动重新运行 SQL 查询以从数据库中获取刷新的结果集?如果查询很耗时怎么办?

Questions: when I view the next row with action_id=2, should I see 'Bill' or 'William'? If 'William', does this mean that saving row 1 has to automatically update 'Bill' to 'William' in all other rows in this result set? Or does it mean that save() automatically re-runs the SQL query to get a refreshed result set from the database? What if the query is time-consuming?

还要考虑面向对象的设计.每行都是一个单独的对象.在一个对象上调用 save() 会产生改变单独对象中的值的副作用(即使它们是同一对象集合的一部分)是否合适?对我来说,这似乎是一种内容耦合.

Also consider the object-oriented design. Each Row is a separate object. Is it appropriate that calling save() on one object has the side effect of changing values in a separate object (even if they are part of the same collection of objects)? That seems like a form of Content Coupling to me.

上面的例子是一个相对简单的查询,但也允许更复杂的查询.Zend_Db 不能分析查询以区分可写结果和只读结果.这也是 MySQL 视图不可更新的原因.

The example above is a relatively simple query, but much more complex queries are also permitted. Zend_Db cannot analyze queries with the intention to tell writable results from read-only results. That's also why MySQL views are not updateable.

这篇关于自动连接表而不破坏 Zend Framework 中的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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