Lithium:如何在表单中显示相关数据然后保存?

发布于 2025-01-06 10:14:13 字数 2385 浏览 0 评论 0原文

我正在使用 Lithium 和 MySQL。 我有一个 hasOne 联系人的用户模型。 联系人模型belongsTo 用户。

我在下面列出了我的代码的一个非常基本的版本。

我的问题:

  1. 当我编辑用户并提交表单时,如何让 Users::edit 也保存联系人数据?
  2. 另外,如何在用户编辑视图中显示contacts.email?

模型/Users.php

<?php
namespace app\models;

class Users extends \lithium\data\Model {

    public $hasOne = array('Contacts');

    protected $_schema = array(
        'id'   => array('type' => 'integer',
                        'key'  => 'primary'),
        'name' => array('type' => 'varchar')
    );
}
?>

模型/Contacts.php

<?php
namespace app\models;

class Contacts extends \lithium\data\Model {

    public $belongsTo = array('Users');

    protected $_meta = array(
        'key'   => 'user_id',
    );

    protected $_schema = array(
        'user_id' => array('type' => 'integer',
                           'key'  => 'primary'),
        'email'   => array('type' => 'string')
    );
}
?>

控制器/UsersController.php

<?php
namespace app\controllers;

use app\models\Users;

class UsersController extends \lithium\action\Controller {
    public function edit() {
        $user = Users::find('first', array(
                'conditions' => array('id' => $this->request->id),
                'with'       => array('Contacts')
            )
        );

        if (!empty($this->request->data)) {
            if ($user->save($this->request->data)) {
                //flash success message goes here
                return $this->redirect(array('Users::view', 'args' => array($user->id)));
            } else {
                //flash failure message goes here
            }
        }
        return compact('user');
    }
}
?>

视图/users/edit.html.php

<?php $this->title('Editing User'); ?>
<h2>Editing User</h2>
<?= $this->form->create($user); ?>
    <?= $this->form->field('name'); ?>
    <?= $this->form->field('email', array('type' => 'email')); ?>
<?= $this->form->end(); ?>

I'm using Lithium with MySQL.
I have a Users model that hasOne Contacts.
The Contacts model belongsTo Users.

I've listed a very basic version of my code, below.

My questions:

  1. When I edit a user and submit the form, how do I make Users::edit save the Contact data, as well?
  2. Also, how do I display contacts.email in the Users edit view?

models/Users.php

<?php
namespace app\models;

class Users extends \lithium\data\Model {

    public $hasOne = array('Contacts');

    protected $_schema = array(
        'id'   => array('type' => 'integer',
                        'key'  => 'primary'),
        'name' => array('type' => 'varchar')
    );
}
?>

models/Contacts.php

<?php
namespace app\models;

class Contacts extends \lithium\data\Model {

    public $belongsTo = array('Users');

    protected $_meta = array(
        'key'   => 'user_id',
    );

    protected $_schema = array(
        'user_id' => array('type' => 'integer',
                           'key'  => 'primary'),
        'email'   => array('type' => 'string')
    );
}
?>

controllers/UsersController.php

<?php
namespace app\controllers;

use app\models\Users;

class UsersController extends \lithium\action\Controller {
    public function edit() {
        $user = Users::find('first', array(
                'conditions' => array('id' => $this->request->id),
                'with'       => array('Contacts')
            )
        );

        if (!empty($this->request->data)) {
            if ($user->save($this->request->data)) {
                //flash success message goes here
                return $this->redirect(array('Users::view', 'args' => array($user->id)));
            } else {
                //flash failure message goes here
            }
        }
        return compact('user');
    }
}
?>

views/users/edit.html.php

<?php $this->title('Editing User'); ?>
<h2>Editing User</h2>
<?= $this->form->create($user); ?>
    <?= $this->form->field('name'); ?>
    <?= $this->form->field('email', array('type' => 'email')); ?>
<?= $this->form->end(); ?>

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

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

发布评论

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

评论(1

南薇 2025-01-13 10:14:13

没有多少人知道这一点,但使用锂,您可以将一个表单绑定到多个对象。

在您的控制器中,返回用户和联系人对象。然后在您的表单中:

<?= $this->form->create(compact('user', 'contact')); ?>

然后将字段表单呈现为特定对象,如下所示:

<?= $this->form->field('user.name'); ?>
<?= $this->form->field('contact.email'); ?>

当用户提交表单时,两个对象的数据将存储为:

$this->request->data['user'];
$this->request->data['contact'];

您可以像平常一样使用此信息来更新数据库。如果您只想在两个对象的数据都有效的情况下保存信息,您可以像这样调用验证:

$user = Users::create($this->request->data['user']);
if($user->validates()) {
    $userValid = true;
}

$contact = Contacts::create($this->request->data['contact']);
if($contact->validates()) {
    $contactValid = true;
}

if($userValid && $userValid){
    // save both objects
}

希望有帮助:)

Not many people know this but with lithium you can bind a form to multiple objects.

In your controller, return both a user and a contact object. Then in your form:

<?= $this->form->create(compact('user', 'contact')); ?>

You then render a field form a specific object like this:

<?= $this->form->field('user.name'); ?>
<?= $this->form->field('contact.email'); ?>

When the user submits the form, the data for both objects will be stored as:

$this->request->data['user'];
$this->request->data['contact'];

You can use this information to update the database as you would normally. If you only want to save the information if data from both objects are valid, you can call validate like this:

$user = Users::create($this->request->data['user']);
if($user->validates()) {
    $userValid = true;
}

$contact = Contacts::create($this->request->data['contact']);
if($contact->validates()) {
    $contactValid = true;
}

if($userValid && $userValid){
    // save both objects
}

Hope that helps :)

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