如何在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 Model类:
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();
答案 0 :(得分:0)
你看过了吗;
class Show extends Phalcon\Mvc\Model
{
public $id;
public $length;
public $title;
public function initialize()
{
$this->hasMany("id", "Episode", "showid", NULL);
}
}
或类似的东西?