将信息保存在“sub”中 CakePHP 中的模型

发布于 2024-07-05 23:48:20 字数 506 浏览 10 评论 0原文

我有一个简单的 CakePHP 站点 (1.2)。 我有一个页面,您可以在其中编辑和保存人员。 所以我有一个 Person 模型和控制器。

在评论表中,每个人都没有或有更多评论。 所以我有一个 Comment 模型,并且我的 Person 模型与 Comment 模型之间有一个 hasMany 关联。 View 运行良好。

我的问题是,在查看“人员”页面上,我有一个添加评论按钮。 这应该如何运作? 我是否应该期望人员控制器包含评论记录的保存,或者创建一个评论控制器并将其保存在人员关联之外?

我对 PHP 有丰富的经验,但对 Cake 来说还是个新手。

有任何想法吗? 我想我只是错过了一些明显的事情,但我不知道该怎么做。 我觉得如果这是 PHP 我会在添加评论表单中引用 Person_id,从而使用单独的控制器,但我觉得为简单模型提供控制器是没有用的,因为评论只是在人员记录的上下文中进行编辑。

有想法吗?

I've got a simple CakePHP site (1.2). I've got a page where you can edit and save a Person. So I have a Person model and controller.

Each Person has none or more comments, in the comment table. So I have a Comment model, and I have a hasMany association on my Person model to the Comment model. View is working great.

My question is, on the view Person page, I have an add comment button. How should this work? Should I expect the Person controller to include a save for the comment record, or create a comment controller and save it outside of it's association for a person?

I'm experienced with PHP, but brand new to Cake.

Any ideas? I think I'm just missing something obvious, but I'm not sure what to do. I feel like if this was PHP I would reference the Person_id in my add comment form, and thus use a separate controller, but I feel like having a controller for a simple Model is useless, since Comments are only edited in the context of a Person record.

Ideas?

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

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

发布评论

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

评论(1

听你说爱我 2024-07-12 23:48:20

我不是 CakePHP 专家,但我仍然认为拥有自己的控制器是有意义的。 我记得在做其中一个 CakePHP 博客教程时,您需要在评论模型中链接评论和帖子。 这是我从中得到的一些代码:

class Comment extends AppModel
{
  var $name = ‘Comment’;
  var $belongsTo = array(‘Person’);
}

然后你需要一个控制器(comments_controller.php):

class CommentsController extends AppController
{
  var $name = ‘Comments’;
  var $scaffold;
}

一些 SQL:

CREATE TABLE comments (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  author VARCHAR(50),
  comment TEXT,
  person_id INT,
  created DATETIME DEFAULT NULL,
  modified DATETIME DEFAULT NULL
);

$scaffold 为你创建一个 CRUD 应用程序,所以当你转到 /comments 时在浏览器中,您可以创建创建、阅读u更新和删除评论。 所以,如您所见,这里没有太多涉及。 您所需要的只是数据库表和一些提供 person_id 的逻辑。

保存评论(在您的 Person/view 中):

<h2>Add comment</h2>
<?php
echo $form->create(‘Comment’, array(‘action’=>‘add/’.$person[‘Person’][‘id’]);
echo $form->input(‘author’);
echo $form->input(‘content’);
echo $form->submit(‘Add comment’);
echo $form->end();
?>

在您的 CommentsController 中:

function add($id = NULL) {
  if (!empty($this->data)) {
    $this->data['Comment']['person_id'] = $id;
    $this->data['Comment']['id'] = '';
    if ($this->Comment->save($this->data)) {
      $this->Session->setFlash('Commented added');
      $this->redirect($this->referer());
    }
  }
}

所以您基本上覆盖了 Cake 本身添加的标准添加操作。 希望现在这是有道理的。 另外,您可能需要一条路线,以便它获取 /comments/add/ID。 我不知道这部分。 :)

I'm not a CakePHP expert, but I still think it would make sense to have your own controller. From what I remember from doing one of those CakePHP blog tutorials is, that you need to link the comments and the post in the comment model. This is some of the code I have from it:

class Comment extends AppModel
{
  var $name = ‘Comment’;
  var $belongsTo = array(‘Person’);
}

And then you need a controller (comments_controller.php):

class CommentsController extends AppController
{
  var $name = ‘Comments’;
  var $scaffold;
}

Some SQL:

CREATE TABLE comments (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  author VARCHAR(50),
  comment TEXT,
  person_id INT,
  created DATETIME DEFAULT NULL,
  modified DATETIME DEFAULT NULL
);

The $scaffold creates a CRUD application for you, so when you go to /comments in your browser you can create, read, update and delete comments. So, as you see, there is not much involved here. All you need is your database tables and a little logic to provide person_id.

To save a comment (in your Person/view):

<h2>Add comment</h2>
<?php
echo $form->create(‘Comment’, array(‘action’=>‘add/’.$person[‘Person’][‘id’]);
echo $form->input(‘author’);
echo $form->input(‘content’);
echo $form->submit(‘Add comment’);
echo $form->end();
?>

And in your CommentsController:

function add($id = NULL) {
  if (!empty($this->data)) {
    $this->data['Comment']['person_id'] = $id;
    $this->data['Comment']['id'] = '';
    if ($this->Comment->save($this->data)) {
      $this->Session->setFlash('Commented added');
      $this->redirect($this->referer());
    }
  }
}

So you basically overwrite the standard add action, which Cake adds by itself. Hope that makes sense now. Also, you might need a route so it picks up /comments/add/ID. I don't know about this part. :)

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