Yii 中模型之间的关系
我有这样的数据库
+------------------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的发票项目在表描述中没有名为invoice_id 的字段。这些关系需要引用主键才能发挥作用。您在关系中声明的数据库字段应包含您正在使用的模型的主键。因此,如果您正在使用发票,HAS_MANY 关系应该能够在项目的invoice_id 字段中找到发票的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:
You can read more about the relations here