尝试在 php fat free 框架和 mysql 中插入带有外键的记录时出现问题
Problem trying to insert records with foreign keys in php fat free framework and mysql
我需要在 usuarios
table 中插入记录,然后在 personas
table 中使用外键 id_usuario
最后插入也使用外键 id_usuario
在 usuarios_roles
table 中记录。我正在使用 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>
我必须遵循这个关系模型,它将 table personas
与 table usuarios
依次连接 usuarios_roles
。主键都是自增的:(
Relational MySQL Model)
现在是我不懂如何编程的部分。在做 save()
时我可以注册一个用户,但是当我做 save()
of persona 时显然它不会链接到 id_usuario
并且不会有 INSERT
在 table usuarios_roles
中。目前我在 index.php 中收到的信息特别是在这里:
$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 查询?谢谢
您可能想要的东西在名为 f3-cortex
的插件中可用。在那里,有一个特定的部分是关于关系的,这可能对你想要完成的事情有好处。 https://github.com/ikkez/f3-cortex#relations 您可以设置 1:1、1:many、many:many 关系来满足您的需求。
With Cortex you can create associations between multiple models. By linking them together, you can create all common relationships you need for smart and easy persistence.
我需要在 usuarios
table 中插入记录,然后在 personas
table 中使用外键 id_usuario
最后插入也使用外键 id_usuario
在 usuarios_roles
table 中记录。我正在使用 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>
我必须遵循这个关系模型,它将 table personas
与 table usuarios
依次连接 usuarios_roles
。主键都是自增的:(
Relational MySQL Model)
现在是我不懂如何编程的部分。在做 save()
时我可以注册一个用户,但是当我做 save()
of persona 时显然它不会链接到 id_usuario
并且不会有 INSERT
在 table usuarios_roles
中。目前我在 index.php 中收到的信息特别是在这里:
$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 查询?谢谢
您可能想要的东西在名为 f3-cortex
的插件中可用。在那里,有一个特定的部分是关于关系的,这可能对你想要完成的事情有好处。 https://github.com/ikkez/f3-cortex#relations 您可以设置 1:1、1:many、many:many 关系来满足您的需求。
With Cortex you can create associations between multiple models. By linking them together, you can create all common relationships you need for smart and easy persistence.