简体   繁体   English

在Phalcon中扩展模型

[英]Extending a Model in Phalcon

How would one make a child-parent relationship between Phalcon MVC models? 如何在Phalcon MVC模型之间建立子代关系?

This is a DB schema I have in mind: 我想到的是一个数据库模式:

explain show;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| length  | int(11)      | NO   |     | NULL    |       |
| title   | varchar(100) | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+

explain show_episode;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| show_id | int(11)      | NO   | PRI | NULL    |       |
| season  | int(11)      | NO   |     | NULL    |       |
| episode | int(11)      | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+

Each element in show_episode is also present in show table, however there are show records which are only present in show . show_episode每个元素也显示在show表中,但是有show记录仅显示在show

The idea is to have two Phalcon Model classes: 这个想法是有两个Phalcon Model类:

class Show extends Phalcon\Mvc\Model
{
  public $id;
  public $length;
  public $title;
}

class ShowEpisode extends Show 
{
  public $season;
  public $episode;
}

How would I need to configure those models to be able to retrieve and save episode records like this: 我将如何配置这些模型以能够检索和保存情节记录,如下所示:

// retrieve
$episode = ShowEpisode::findFirst(array("id"=>333));
echo $episode->season;
echo $episode->title;

// save
$episode->title = "New Title";
$episode->season = 3;
$episode->save();

Have you looked at; 你看过了吗?

class Show extends Phalcon\Mvc\Model
{
  public $id;
  public $length;
  public $title;

  public function initialize()
  {
    $this->hasMany("id", "Episode", "showid", NULL);
  }
}

Or something similar? 还是类似的东西?

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

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