私はMySQLでLithiumを使用しています。 ユーザーモデルがhasOne
になりました。 連絡先モデルbelongsTo
ユーザー。リチウム:関連するデータをフォームに表示して保存するにはどうすればよいですか?
私は以下のコードの非常に基本的なバージョンをリストアップしました。
私の質問:
- 私は、ユーザーを編集してフォームを送信すると、どのように私も、連絡先データのセーブユーザー::編集を作るのですか?
- また、ユーザーの編集ビューで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');
}
}
?>
ビュー/ユーザー/ edit.html。 PHP
を助けること誰か助けてくれるかもしれない! –
それだけでした! :) – Oerd
ありがとうマックス。そのような偉大な詳細で返信するあなたの非常に種類: – Housni