Yii 中模型之间的关系

发布于 2025-01-06 21:05:05 字数 1203 浏览 1 评论 0原文

我有这样的数据库

+------------------+
|     Invoices     |
+------------------+
| id               |
| customer_id (Fk) |
| description      |
+------------------+

+------------------+
|   InvoiceItems   |
+------------------+
| id               |
| Item_name        |
| price            |
| discount         |
| description      |
+------------------+

+------------------+
|    Customers     |
+------------------+
| id               |
| firstname        |
| lastname         |
| description      |
+------------------+

根据我在模型中建立的关系如下。
在 Invoices 模型中,关系是这样

public function relations()
{
 return array(
  'invoiceitem' => array(self::HAS_MANY,'InvoiceItems','invoice_id'),
  'customers'    =>  array(self::BELONGS_TO,'Customer','customer_id'),
 );
}

在 InvoiceItems 模型中,关系是这样

public function relations()
{
 return array(
   'invoice' => array(self::BELONGS_TO,'Invoices','invoice_id'),
 );
}

在 Customers 模型中,关系是这样

public function relations()
{
  return array(
   'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
  );
}

现在我想知道这些模型之间的关系是否正确?

I have the database like this

+------------------+
|     Invoices     |
+------------------+
| id               |
| customer_id (Fk) |
| description      |
+------------------+

+------------------+
|   InvoiceItems   |
+------------------+
| id               |
| Item_name        |
| price            |
| discount         |
| description      |
+------------------+

+------------------+
|    Customers     |
+------------------+
| id               |
| firstname        |
| lastname         |
| description      |
+------------------+

According to the database I made relations in the models as follows.
In Invoices model,the relation is like this

public function relations()
{
 return array(
  'invoiceitem' => array(self::HAS_MANY,'InvoiceItems','invoice_id'),
  'customers'    =>  array(self::BELONGS_TO,'Customer','customer_id'),
 );
}

In InvoiceItems model relation is like this

public function relations()
{
 return array(
   'invoice' => array(self::BELONGS_TO,'Invoices','invoice_id'),
 );
}

In Customers model the relations is like this

public function relations()
{
  return array(
   'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
  );
}

Now I want to know if the relations between these models are right or not?

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

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

发布评论

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

评论(1

殤城〤 2025-01-13 21:05:05

您的发票项目在表描述中没有名为invoice_id 的字段。这些关系需要引用主键才能发挥作用。您在关系中声明的数据库字段应包含您正在使用的模型的主键。因此,如果您正在使用发票,HAS_MANY 关系应该能够在项目的invoice_id 字段中找到发票的id。
您还可以为关系数据使用单独的表,并如下声明:

array(self::HAS_MANY,'InvoiceItems',reltable('invoice_id', 'item_id))

您可以阅读有关关系的更多信息 此处

Your Invoice item does not have a field called invoice_id in the table description. The relations need a reference to the primary key to work. The db-field you declare in the relation should contain the primary key of the model you are working with. So if you are working with a invoice, the HAS_MANY relation should be able to find the id of the invoice in the invoice_id field on the item.
You could also use a separate table for the relation data and declare it like:

array(self::HAS_MANY,'InvoiceItems',reltable('invoice_id', 'item_id))

You can read more about the relations here

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