简体   繁体   English

Phalcon PHP:初始化时动态设置模型源

[英]Phalcon PHP: Set model source dynamically on initialize

I've got a few simple EAV tables that all have the same structure, but are for different purposes: 我有一些简单的EAV表,它们都具有相同的结构,但是具有不同的用途:

item_attribute
id | attribute_name | size_limit

item_attribute_map
id | attr_id | source_id | value

And: 和:

transaction_attribute
id | attribute_name | size_limit

transaction_attribute_map
id | attr_id | source_id | value

The logic that will use these is for all intensive purposes identical, but it will be referencing different tables obviously. 用于所有密集用途的逻辑将完全相同,但是显然将引用不同的表。

So, I decided I'd simply make a model that could have it's source changed, something like this: 因此,我决定只制作一个可以更改其源代码的模型,如下所示:

class Attribute extends \Phalcon\Mvc\Model {
    public $id;
    public $attr_id;
    public $source_id;
    public $value;

    public function getSource($param){
        return $param.'_attribute_map';
    }
}

I've considered overriding _ construct(), passing a parameter, setting it, calling parent:: _construct() and being done with it; 我考虑过重写_ Construct(),传递参数,设置参数,调用parent :: _construct()并完成它; but that doesn't seem remotely elegant. 但这似乎并不优雅。 Is there any way to pass a parameter to initialize()? 有什么办法可以将参数传递给initialize()吗? Should I be doing this another way? 我应该以其他方式这样做吗? I'm not in to the idea of doing all this work in the controller, because these two table sets to wildly different things, and I'd like to use this sort of database technique further. 我不打算在控制器中完成所有这些工作,因为这两个表设置了完全不同的东西,并且我想进一步使用这种数据库技术。

Any help would be appreciated, and let me know if I wasn't clear and I'll edit. 任何帮助将不胜感激,如果不清楚,请告诉我,我将进行编辑。

EDIT: Well I tried my inelegant solution of using __construct() and determined that it's a final method, and now I'm actually out of ideas. 编辑:好吧,我尝试了使用__construct()的出色解决方案,并确定这是一种最终方法,现在我实际上是个主意。

EDIT 2: Well I've found a way to do what I want (sort of), which is by calling setSource() after the model is instantiated. 编辑2:好吧,我已经找到一种方法来做我想要的(某种),这是通过在实例化模型后调用setSource()来实现的。 I'm not a big fan of this though because it's linking the model to a table, and then immediately linking it to another table afterwards. 我不是很喜欢这个,因为它是将模型链接到表,然后立即将其链接到另一个表。

EDIT 3: I've run across the idea of setting up a base class that will accept creation requests for three separate models, but in those cases will actually only create one model, and handle method requests accordingly. 编辑3:我遇到过建立一个基类的想法,该基类将接受三个独立模型的创建请求,但在那种情况下,实际上只会创建一个模型,并相应地处理方法请求。 This is definitely a better solution than the last, but I don't like having global rules for specific cases, I'd rather have specific rules for specific cases within the scope of those situations. 这绝对是比上一个更好的解决方案,但是我不喜欢针对特定情况制定全局规则,而是希望在那些情况的范围内针对特定案例制定特定规则。 I'll use this method for now, but I'll leave this question open in the hopes that someone has a better idea. 我现在将使用此方法,但我会保留此问题,以希望有人有更好的主意。

if you want same methods to be available on some models, just create base model and extend it in your models instead of extending \\Phalcon\\Mvc\\Model 如果要在某些模型上使用相同的方法,只需创建基本模型并在模型中扩展它,而不是扩展\\ Phalcon \\ Mvc \\ Model

class BaseModel extends \Phalcon\Mvc\Model {
  // functions what you need in several other models
}

class Attribute extends BaseModel  {
  // functions what you need in several other models
}

class Attribute extends BaseModel  {
  // functions what you need in several other models
}

I don't understand if you tried this option, or not? 我不知道您是否尝试过此选项? Maybe you done something similar? 也许您做了类似的事情? You should add more code in your question. 您应该在问题中添加更多代码。

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

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