简体   繁体   English

使用1个对象表示2个模型

[英]Using 1 Object To Represent 2 Models

We are building a core shopping cart that our company will use as a foundation for multiple shopping carts we will build. 我们正在构建一个核心购物车,我们公司将以此为基础来构建多个购物车。 These are highly specialized, so different product types will require their own tables of data. 这些是高度专业化的,因此不同的产品类型将需要自己的数据表。

For instance, for a cart that sells labels... 例如,对于出售标签的购物车...

product - id | type_id | created 产品 id | type_id | created id | type_id | created

label - id | product_id | x | y | z 标签 id | product_id | x | y | z id | product_id | x | y | z

We're struggling with how to structure our objects. 我们正在努力如何构造对象。 We'd like to programmatically only interact with the Label class and have the data be "split" so to speak between the two tables. 我们只想通过编程方式与Label类交互,并“拆分”数据,以便在两个表之间进行交流。 One idea we tossed around was creating a view to use for querying and then just overwriting the object's save() method to actually interact with each table's setters/save functionality. 我们抛弃的一个想法是创建一个用于查询的视图 ,然后覆盖该对象的save()方法以实际与每个表的setter / save功能进行交互。

Has anyone accomplished this or at least faced a similar challenge? 有没有人做到这一点或至少面临类似的挑战?

Update: Of course this begs the question... is there a scenario where both tables might have the same column name? 更新:当然,这引出了一个问题……是否存在两个表可能具有相同列名的情况? And if so, how to handle it. 如果是这样,该如何处理。

You can use name/value pair for specialized columns. 您可以将名称/值对用于专用列。 With "name" part consistent with column names, you can have generalized setter/getter. 通过使“名称”部分与列名称一致,您可以具有通用的setter / getter。

Product is related table, so you can interact with it via relation, for instance $labelModel->product . 产品是相关表,因此您可以通过关系与其进行交互,例如$labelModel->product So you have model for each table, as yii suggest. 因此,正如yii所建议的,您具有每个表的模型。 And you can interact only with Label model if you place your functionality at beforeSave() , afterSave() and other methods. 如果将功能放在beforeSave()afterSave()和其他方法上,则只能与Label模型进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM