简体   繁体   中英

Phalcon\Mvc\Model relationship with abstract class

I am working with Phalcon's MVC Models and would like to take advantage of object relationship.

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. Let's say the columns are id , type and label .
  2. I have an abstract class:

    class AbstractHtmlFormElements extends Phalcon\\MVC\\Model {}

  3. I have a set of classes, one for each type of HTML form element:

    class Text extends AbstractHtmlFormElements {}

    class Date extends 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.

Is it possible to load all dependent classes in this situation to take advantage of $htmlPage->getRelated() functionality?

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". 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:

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. if you are using model layer, don't think too much about actual 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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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