作者:sawrf12454_191 | 来源:互联网 | 2023-05-16 10:00
Is there any way in Kohana 3's ORM to run a chunk of code in a model, but only after that model has been loaded from the database? A simple example is a required has_one relationship.
在Kohana 3的ORM中是否有任何方法可以在模型中运行一大块代码,但只有在从数据库加载该模型之后?一个简单的例子是必需的has_one关系。
ORM::factory('user')->where('name', '=', 'Bob')->find();
Now what if all users have to have some other property, so if Bob doesn't exist, it will have to be created? Right now, in the place where this line is running, I'm checking for null primary key, and instructing the model to add that relationship if so. But is there any way to have it done by the model? The problem with the constructor is that models can be constructed empty just before being populated from the DB, as is visible in this example, so I don't want that.
现在如果所有用户都必须拥有其他属性,那么如果Bob不存在,则必须创建它?现在,在这一行运行的地方,我正在检查空主键,并指示模型添加该关系,如果是这样的话。但有没有办法让模型完成它?构造函数的问题是模型可以在从DB填充之前构造为空,如本示例中所示,因此我不希望这样。
2 个解决方案