有关如何在 Symfony 中嵌入表单的示例

发布于 2024-08-23 03:36:06 字数 1316 浏览 6 评论 0原文

我在 Ubuntu 9.10 上使用 Symfony 1.3.2 和 Propel ORM。

我有一个用户配置文件表,其中有许多其他表链接到它(即 user_profile.id 是许多其他表中的 FK。

我的数据库架构如下所示:

user_profile:
  _attributes: { phpName: UserProfile }
  id: ~
  guard_id:  { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true }
  address:   { type: longvarchar, required: true }

vehicle_type:
  _attributes: { phpName: VehicleType }
  id: ~
  name: { type: varchar(32), required: true }


user_vehicle:
  _attributes: { phpName: UserVehicle }
  id: ~
  user_id:  { type: integer, foreignTable: user_profile, foreignReference: id, required: true }
  vehicle_type: { type: integer, foreignTable: vehicle_type, foreignReference: id, required: true }
  license_plate:     { type: varchar(16), required: true }


user_child:
  _attributes: { phpName: UserChild }
  id: ~
  user_id:  { type: integer, foreignTable: user_profile, foreignReference: id, required: true }
  gender:   { type: boolean, required: true }
  name:     { type: varchar(32), required: true }

我想嵌入链接到用户配置文件的其他对象 ,相关对象(例如 UserVehicle、UserJob 也与用户配置文件对象同时进行 CRUD)。

对象,在用户配置文件表单中,以便当我对用户配置文件表单执行 CRUD时 演示如何:

  1. 将各种相关对象(即 UserVehicle、UserChild)嵌入到 UserProfile 表单中
  2. 在执行操作时创建/更新/删除各种相关对象(请注意,一个用户可以拥有 0-N 多个车辆或孩子分配给他们

I am using Symfony 1.3.2 with Propel ORM on Ubuntu 9.10.

I have a user profile table, which has many other tables linked to it (i.e. user_profile.id is a FK in many other tables.

My db schema looks something like this:

user_profile:
  _attributes: { phpName: UserProfile }
  id: ~
  guard_id:  { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true }
  address:   { type: longvarchar, required: true }

vehicle_type:
  _attributes: { phpName: VehicleType }
  id: ~
  name: { type: varchar(32), required: true }


user_vehicle:
  _attributes: { phpName: UserVehicle }
  id: ~
  user_id:  { type: integer, foreignTable: user_profile, foreignReference: id, required: true }
  vehicle_type: { type: integer, foreignTable: vehicle_type, foreignReference: id, required: true }
  license_plate:     { type: varchar(16), required: true }


user_child:
  _attributes: { phpName: UserChild }
  id: ~
  user_id:  { type: integer, foreignTable: user_profile, foreignReference: id, required: true }
  gender:   { type: boolean, required: true }
  name:     { type: varchar(32), required: true }

I would like to embed the other objects that link to the user profile object, in the user profile form, so that when I am performing CRUD on a user profile form, the related objects (e.g. UserVehicle, UserJob are also CRUD at the same time as the user profile object).

I need a simple snippet that will show how to:

  1. Embed the various related objects (i.e. UserVehicle, UserChild) into the UserProfile form
  2. Create/Update/Delete the various related objects as the operation is being carried (please note, a user can have more than 0-N vehicles or children assigned to them

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

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

发布评论

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

评论(2

疯狂的代价 2024-08-30 03:36:06

您是否阅读过文档?:

// lib/form/doctrine/ProductForm.class.php
public function configure()
{
  $subForm = new sfForm();
  for ($i = 0; $i < 2; $i++)
  {
    $productPhoto = new ProductPhoto();
    $productPhoto->Product = $this->getObject();

    $form = new ProductPhotoForm($productPhoto);

    $subForm->embedForm($i, $form);
  }
  $this->embedForm('newPhotos', $subForm);
}

对于创建/删除/更新部分,这篇文章可能会提供一些帮助。

Have you read the documentation?:

// lib/form/doctrine/ProductForm.class.php
public function configure()
{
  $subForm = new sfForm();
  for ($i = 0; $i < 2; $i++)
  {
    $productPhoto = new ProductPhoto();
    $productPhoto->Product = $this->getObject();

    $form = new ProductPhotoForm($productPhoto);

    $subForm->embedForm($i, $form);
  }
  $this->embedForm('newPhotos', $subForm);
}

For the create/delete/update part, this article might give some help.

温柔嚣张 2024-08-30 03:36:06

我从未找到适合我需求的官方方法。我开发了一种完全不同的方法。在我曾经工作的公司中,我们在生产中使用 这种新方法,发现它更有弹性和简单。关键概念是“不要使用 Symfony 的 Form 类,你会发现嵌入表单可以是一个非常简单的任务”
我希望这可以帮助您嵌入表单。

I never found the official approach right for my needs. I developed a completely different approach. In the company where I used to work we used in production this new approach, finding it a bit more elastic and simple. The key concept is "don't use Symfony's Form class and you will discover that embedding forms can be a very simple task"
I hope this can help you embedding forms.

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