简体   繁体   English

与抽象类的Phalcon \\ Mvc \\ Model关系

[英]Phalcon\Mvc\Model relationship with abstract class

I am working with Phalcon's MVC Models and would like to take advantage of object relationship. 我正在使用Phalcon的MVC模型,并想利用对象关系。

There's a bit of complication involved in my case. 我的案子有些复杂。 I have a database table that serves as storage and I have Models that extend it. 我有一个用作存储的数据库表,并且有扩展它的模型。

  1. I have a DB table "html_form_elements" that describes HTML form elements of all types. 我有一个数据库表“ html_form_elements”,该表描述了所有类型的HTML表单元素。 Let's say the columns are id , type and label . 假设这些列是idtypelabel
  2. I have an abstract class: 我有一个抽象类:

    class AbstractHtmlFormElements extends Phalcon\\MVC\\Model {} class AbstractHtmlFormElements扩展了Phalcon \\ MVC \\ Model {}

  3. I have a set of classes, one for each type of HTML form element: 我有一组类,每种类型的HTML表单元素都有一个类:

    class Text extends AbstractHtmlFormElements {} Text扩展AbstractHtmlFormElements {}

    class Date extends AbstractHtmlFormElements {} Date扩展AbstractHtmlFormElements {}

I would like to have a "container" class HtmlPage which would link Text , Date and all other specific object using $this->hasMany() type of relationship. 我想有一个“容器”类HtmlPage,它将使用$ this-> hasMany()类型的关系链接TextDate和所有其他特定对象。

Is it possible to load all dependent classes in this situation to take advantage of $htmlPage->getRelated() functionality? 在这种情况下是否可以加载所有依赖类以利用$ htmlPage-> getRelated()功能?

I don't want to describe relationship between HtmlPage and every question type separately, as it would create redundant queries against the same table "html_form_elements". 我不想单独描述HtmlPage和每个问题类型之间的关系,因为它会针对相同的表“ html_form_elements”创建冗余查询。 Is it possible to load all rows describing different Models with one query? 是否可以通过一个查询加载描述不同模型的所有行?

Thanks! 谢谢!

here is an example of some simultaneous relations form 1 model: 这是表格1模型的一些同时关系示例:

class Model_UserTest extends \Phalcon\Mvc\Model {
public function initialize() {
    $this->hasManyToMany(
            "id", "Model_TagToTest", "test_id", "tag_id", "Model_UserTag", "id", array(
        'alias' => 'tags'
    ));
    $this->hasManyToMany(
            "id", "Model_QuestionToTest", "test_id", "question_id", "Model_UserQuestion", "id", array(
        'alias' => 'questions'
    ));
    //  $this->hasMany("id", "RobotsParts", "robots_id");
    $this->hasOne(
            "id", "Model_UserTestPrintSettings", "test_id", array(
        'alias' => 'printSettings'
    ));
}
    }

what i use to get related is the alias. 我用来关联的是别名。 for example 例如

$m = Model_UserTest::findFirst();
$m->tags; // to get all related tags from model Model_UserTag they are connected by table Model_TagToTest

Describe all the connections you need. 描述您需要的所有连接。 Phalcon is smart enough to make sql work as fast as possible. Phalcon足够聪明,可以使sql尽可能快地工作。 if you are using model layer, don't think too much about actual sql. 如果您使用的是模型层,请不要对实际的SQL考虑太多。 after developing your application, always collect slow queries logs and if there are some - add indexes. 开发应用程序后,请始终收集慢查询日志,如果有,请添加索引。 99% times it's all you need to keep your webpage respond fastly. 保持网页快速响应所需要的99%倍。

In this case my round up would be: Do not think about a problem, when you don't have it. 在这种情况下,我的总结将是:没有问题时不要考虑问题。

by the way, for server's monitoring i recommend New Relic service http://newrelic.com/ 顺便说一句,对于服务器的监视,我建议使用New Relic服务http://newrelic.com/

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

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