I'm new to PHP and I'm working on Symfony 4 project
I've made a Java project using an Enum Class called role
public class Role {
public enum RoleEnum {
Admin, SuperAdmin, Etudiant, Entreprise } private Short id;
private RoleEnum name;
public Role(Short id, RoleEnum name) {
this.id = id;
this.name = name ;
}
and in my user I made it like this
public abstract class User {
private Long id;
private String nom;
private String email;
private String mdp;
private String tel;
private String photo;
private Role role;
private Boolean etatCompte;
and I've made a relation between the two tables in MySQL
and I created this method in Java too
public Role getRoleByName(RoleEnum input) {
Statement stm;
try {
stm = cnx.createStatement();
String query = "SELECT id from role Where name='" +input+"'";
ResultSet rst = stm.executeQuery(query);
while (rst.next()) {
Short id = rst.getShort("id");
return new Role(id, input);
}
} catch (SQLException ex) {
Logger.getLogger(ServiceEtudiant.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
and when I'm adding values to the table for example to add "admin" to the role I do it like this
Role role = s.getRoleByName(Role.RoleEnum.Admin);
can anyone help me or guide how to do it the sameway in my symfony project?
My Answer:
"SELECT * FROM Role WHERE name = '" + input.name() + "'";__ ResultSet rs = stm.executeQuery(query);__ if (rs.next()) {_ return new Role(rs.getShort("id"), RoleEnum.valueOf(rs.getString("name")));_ } else {_ return null;_ }_ } catch (SQLException e) {_ e.printStackTrace();_ return null;_ }_}"
Rate this post
3 of 5 based on 5802 votesComments