Lithium:如何在表单中显示相关数据然后保存?
我正在使用 Lithium 和 MySQL。 我有一个 hasOne
联系人的用户模型。 联系人模型belongsTo
用户。
我在下面列出了我的代码的一个非常基本的版本。
我的问题:
- 当我编辑用户并提交表单时,如何让 Users::edit 也保存联系人数据?
- 另外,如何在用户编辑视图中显示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:
- When I edit a user and submit the form, how do I make Users::edit save the Contact data, as well?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有多少人知道这一点,但使用锂,您可以将一个表单绑定到多个对象。
在您的控制器中,返回用户和联系人对象。然后在您的表单中:
然后将字段表单呈现为特定对象,如下所示:
当用户提交表单时,两个对象的数据将存储为:
您可以像平常一样使用此信息来更新数据库。如果您只想在两个对象的数据都有效的情况下保存信息,您可以像这样调用验证:
希望有帮助:)
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:
You then render a field form a specific object like this:
When the user submits the form, the data for both objects will be stored as:
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:
Hope that helps :)