我需要在usuarios表中插入記錄,然后personas使用外鍵id_usuario在usuarios_roles表中插入記錄,最后也使用外鍵將記錄插入表中id_usuario。我正在使用 PHP Fat Free Framework。這是我在視圖 record.html 中注冊角色的表單(請忽略空白輸入):
<form action="registroEmpresa" method="post">
<input type="text" class="form-control" name="rut" />
<input type="text" class="form-control" name="nombre_razonsocial" />
<input type="text" class="form-control" name="telefono" />
<input type="email" class="form-control" name="email" />
<input type="password" class="form-control" name="password" />
<?php $empty = ' '; ?>
<input id="" name="apellidos" type="hidden" value="<?php echo $empty; ?>">
<input id="" name="direccion" type="hidden" value="<?php echo $empty; ?>">
<input id="" name="pais" type="hidden" value="<?php echo $empty; ?>">
<input id="" name="region" type="hidden" value="<?php echo $empty; ?>">
<input id="" name="ciudad" type="hidden" value="<?php echo $empty; ?>">
<input id="" name="comuna" type="hidden" value="<?php echo $empty; ?>">
<button type="submit">Registrarme</button>
</form>
我必須遵循表連接這個關系模型personas與該表usuarios并反過來usuarios_roles。主鍵都是自增的:(
關系 MySQL 模型)
現在到了我不明白如何編程的部分。在做的時候save()我可以注冊一個用戶,但是當我做save()角色的時候顯然它不會鏈接到表中id_usuario也不會有一個。目前我在我的 index.php 中特別收到了資訊:INSERTusuarios_roles
$f3->route('POST @registroEmpresa: /registroEmpresa',
function($f3) {
$db = getConnection();
$usuario = new DB\SQL\Mapper($db, 'usuarios');
$usuario->copyFrom('POST');
$usuario->save(); //insert new usuario row
$persona = new DB\SQL\Mapper($db, 'personas');
$persona->copyFrom('POST');
$persona->save(); //insert new persona row (without foreign key ;C )
//get credentials
$email = $f3->get('POST.email');
$password = $f3->get('POST.password');
//bad practices to add "id_usuario" to personas table
$db->exec("UPDATE `personas`
SET `id_usuario` = (SELECT `id_usuario` FROM `usuarios`
WHERE `email` = '$email' and `password` = '$password')
ORDER BY `id_persona` DESC
LIMIT 1");
//bad practices to add "id_usuario" to usuarios_roles table
$db->exec("INSERT INTO `usuarios_roles`(`id_usuario`, `id_rol`) VALUES
(( SELECT `id_usuario` FROM `usuarios`
WHERE `email` = '$email' and `password` = '$password'ORDER BY `id_usuario` DESC
LIMIT 1),5)");
echo View::instance()->render('login.html');
});
您是否有任何 PHP 無脂肪代碼或想法可以用于此目的,而無需使用更新、插入或 SQL 查詢?謝謝
uj5u.com熱心網友回復:
您可能會在一個名為f3-cortex. 在那里,有一個關于關系的特定部分,可能對您要完成的作業有所幫助。https://github.com/ikkez/f3-cortex#relations您可以設定 1:1、1:many、many:many 關系來滿足您的需求。
使用 Cortex,您可以在多個模型之間創建關聯。通過將它們鏈接在一起,您可以創建智能且輕松的持久性所需的所有常見關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381618.html
下一篇:如何在滑塊下顯示值?
