1

如何在 Phalcon MVC 模型之间建立子父关系?

这是我想到的数据库模式:

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    |       |
+---------+--------------+------+-----+---------+-------+

中的每个元素show_episode也存在于show表中,但是有显示记录仅存在于show.

这个想法是有两个 Phalcon 模型类:

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

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

我需要如何配置这些模型才能像这样检索和保存剧集记录:

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

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

1 回答 1

0

你看过吗?

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

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

或者类似的东西?

于 2013-10-28T10:43:15.810 回答