Yii - 数据库 - 活动记录问题

发布于 2024-09-30 22:07:25 字数 505 浏览 2 评论 0原文

我是 Yii php 框架的新手。我了解 python 框架 - django 并设法创建基于 MVC 模式的应用程序和数据库。现在我想在 php 框架中创建一些其他项目/应用程序 - 我正在尝试 symfony,但现在我想尝试 Yii。我的问题是如何用 Yii 的活动记录语言编写查询。我正在阅读大量文档并尝试使用 Yii 的 shell (yiic shell config/main.php) 进行一些操作,但没有得到我想要的结果。我有三个表 - A、B 和 C,其中 C 表仅包含表 A 和 B 中的外键(它们的 ID)。我想仅使用第三个表(表 C)建立多对多关系。我很困惑如何从表 A 中获取一些项目/记录(例如包含某些名称的记录 - 在 SQL 中,它会类似于... name LIKE xy),然后在表 C 中查找它们的 ID 并找到所有匹配的 ID(对),最后与 B 的 ID 一起从表 B 中提取有关特定 ID 的更多信息。 非常感谢您花时间阅读本文,更感谢您为我提供了一些有用的信息。

PS:如果您发现我的英语有任何问题,请告诉我 - 我想提高我的英语水平。

I am new to Yii php framework. I know python framework - django and managed to create application based on MVC pattern together with database. Now I would like to create some other project/application in php framework - I was trying symfony, but now I want to try the Yii. My problem/question is how to write the queries in Yii's active record language. I am reading a lot of documentation and trying some things using Yii's shell (yiic shell config/main.php), but without getting the results I want. I have three tables - A, B and C where the C table contains only foreign keys from tables A and B (their IDs). I want to have Many-to-Many relationship using just third table (table C). I am puzzled by how can get some items/records from table A (for instance the records which contain some name - in SQL it would be something like ... name LIKE xy), then look for their IDs in table C and find all the matching IDs (pairs) and then finally with IDs of B to extract some more information on specific IDs from table B.
Thanks a lot for your time spent on reading this and yet more thank for providing me with some useful information.

P.S.: if you find anything wrong regarding my english, please let me know - I would like to get better with my english.

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

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

发布评论

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

评论(1

划一舟意中人 2024-10-07 22:07:26

我假设您已经阅读了手册中的这一页(如果没有,请立即阅读!):
http://www.yiiframework.com/doc/guide/1.1/en /database.arr

无论如何,您有两个模型(AModel 和 BModel)和一个如下所示的表结构:

TableA (table for AModel)
-a_id (primary key)
-otherfieldsA

TableB (table for BModel)
-b_id (primary key)
-otherfieldsB

TableC (relation table)
-a_id (foreign key in TableA)
-b_id (foreign key in TableB)

在 AModel 中,添加以下 MANY_MANY 关系:

public function relations()
{
  return array(
    'BModels' => array(self::MANY_MANY, 'BModel', 'TableC(a_id, b_id)')
  );
}

在 BModel 中,添加以下 MANY_MANY 关系:

public function relations()
{
  return array(
    'AModels' => array(self::MANY_MANY, 'AModel', 'TableC(b_id, a_id)')
  );
}

现在,获取所有对于与 AModel 相关的 BModel,您可以这样做:

$myAmodel = AModel::model()->findbyPk('1'); // give an id of an A model here
foreach($myAmodel->BModels as $aBmodel) {  // get all of the related B's and loop
  echo $aBmodel->otherfieldsB; // access data from the related B model
}

反之亦然:

$myBmodel = BModel::model()->findbyPk('1'); // give an id of an B model here
foreach($myBmodel->AModels as $anAmodel) { // get all of the related A's and loop
  echo $anAmodel->otherfieldsA; // access data from the related A model
}

注意:这并不能解决定义和分配新关系的问题,只是从现有关系中获取数据。要创建 MANY_MANY 关系,您将需要一些自定义代码来制作表单选择小部件,并保存和更新关系表(通常在模型的 afterSave() 方法中自定义 SQL)。

Yii 扩展存储库中的一些扩展提供了使此操作变得更容易的行为,如下所示:

I assume you've read this page in the manual already (if not, do it now!):
http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Anyway, you have two models (AModel and BModel) and a table structure like this:

TableA (table for AModel)
-a_id (primary key)
-otherfieldsA

TableB (table for BModel)
-b_id (primary key)
-otherfieldsB

TableC (relation table)
-a_id (foreign key in TableA)
-b_id (foreign key in TableB)

In AModel, add the following MANY_MANY relationship:

public function relations()
{
  return array(
    'BModels' => array(self::MANY_MANY, 'BModel', 'TableC(a_id, b_id)')
  );
}

And in BModel, add the following MANY_MANY relationship:

public function relations()
{
  return array(
    'AModels' => array(self::MANY_MANY, 'AModel', 'TableC(b_id, a_id)')
  );
}

Now, to get all of the BModels related to AModel, you can do this:

$myAmodel = AModel::model()->findbyPk('1'); // give an id of an A model here
foreach($myAmodel->BModels as $aBmodel) {  // get all of the related B's and loop
  echo $aBmodel->otherfieldsB; // access data from the related B model
}

And vica versa:

$myBmodel = BModel::model()->findbyPk('1'); // give an id of an B model here
foreach($myBmodel->AModels as $anAmodel) { // get all of the related A's and loop
  echo $anAmodel->otherfieldsA; // access data from the related A model
}

Note: This does not solve problems of defining and assigning new relations though, just getting data from existing relationships. To create new MANY_MANY relationships you will need some custom code to make the Form selection widgets, and to save and update the relation table (custom SQL in the afterSave() methods of the models, usually).

There are some extensions in the Yii extension repository that provide Behaviors which make this easier though, like the following:

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