一个控制器两种型号

发布于 2024-10-24 08:40:04 字数 316 浏览 2 评论 0原文

假设我有一个 YouTube 风格的网站,但包含图像而不是视频。每个用户一次可以上传多张图像。我想将图像存储在一个表中,并且我想将有关显示图像的页面(图像的“辩论”页面)的信息存储在另一个表中 - 这个表有一列,我在其中存储图像的外部 ID已上传并在数据库中引用的图像。

我应该创建两个模型并从同一个控制器调用它们吗?

例如,我可以让 createDebateController 创建两个模型 - 一个用于存储辩论页面信息,另一个用于存储图像。这合理吗?如果是,我将如何将每个图像的 id 存储在在数据库中创建辩论页面信息的模型中?

Let’s say I have a YouTube-style site, but with images rather than videos. Each user can upload multiple images at a time. I want to store the images in one table, and I want to store the information about the page that displays the images (the images’ “debate” page) in another table — this one has a column where I store the foreign ID of the images that were uploaded and referenced in the database.

Should I create two models and call them both from the same controller?

For example, I could have createDebateController create two models — one for storing the debate page info and one for storing the images. Would this be reasonable? If it is, how would I store the id of each image in the model that creates the debate page info in the DB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-10-31 08:40:04

我将为每个创建一个模型,然后仅通过其相关的图像对象引用辩论对象。因此,当发布新图像时:

$image = new Image();
$image->setImage($file);
$image->setName($name);
$image->save();

然后,每当发布评论时,您将使用图像对象间接创建辩论对象:

$image->addDebate($user, $timestamp, $comment);

或者在渲染显示页面时:

foreach ($image->getDebates() as $debate) {
    $debate->print();
}

您的图像对象将直接访问辩论对象:

class Image
{

    public function addDebate($user, $timestamp, $comment)
    {
        $debate = new Debate();
        $debate->setImageId($this->id);
        $debate->setUser($user);
        $debate->setTimestamp($timestamp);
        $debate->setComment($comment);
        $debate->save();
    }

    public function getDebates()
    {
        return query("select * from debate where image_id = ?", $this->id);
    }

}

I would create a model for each, then only reference the debate object through its related image object. So, when posting a new image:

$image = new Image();
$image->setImage($file);
$image->setName($name);
$image->save();

Then, whenever a comment gets posted, you'd use the image object to indirectly create the debate object:

$image->addDebate($user, $timestamp, $comment);

Or when rendering the display page:

foreach ($image->getDebates() as $debate) {
    $debate->print();
}

Your image object would then access the debate object directly:

class Image
{

    public function addDebate($user, $timestamp, $comment)
    {
        $debate = new Debate();
        $debate->setImageId($this->id);
        $debate->setUser($user);
        $debate->setTimestamp($timestamp);
        $debate->setComment($comment);
        $debate->save();
    }

    public function getDebates()
    {
        return query("select * from debate where image_id = ?", $this->id);
    }

}
何以心动 2024-10-31 08:40:04

我认为无论如何你可能会使用两个单独的控制器。一种用于辩论,一种用于图像。您的辩论模型(假设一对多关系)应该具有图像 ID 的集合,或者如果您需要每个图像的位置或其他标识信息,则可能具有图像描述符的集合。如果您不需要放置信息,则只需使用图像 ID 来使用图像控制器生成每个图像的链接集合。如果您确实需要放置(或描述等信息),则描述模型具有实际的图像 ID。是的,在暴露于视图的模型之间建立关系是可以的。

I think you probably use two separate controllers regardless. One for debates and one for images. Your debate model (assuming a one-to-many relationship) should have a collection of image ids, or maybe a collection of image descriptors if you need placement or other identifying information for each image. If you don't need placement information, you simply use the image ids to generate the collection of links to each image using the image controller. If you do need placement (or description, etc. information) then, the description model has the actual image id. Yes it is ok to have relationships between your models that are exposed to the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文