jeudi 19 septembre 2019

OOP PhP Problem of empty properties of a class while they are not empty when we Insert in databse. PostgreSQL

Let me explain my problem. I manage an app that allows to give roles to Users and give them access to different schemas, with a PostgreSQL database.

Each user is a "new User" object created by my User.class.php with those properties:

class User{
    private $_idUser;
    private $_login;
    private $_password;
    private $_roles;
    private $_schemas;
}

Everything works, I can modify a User or create one, give him access to one or more schemas, assign him one or more roles. Roles and Schemas can be selected in checkboxes. As you can see :

For example, the checkbox roles :

$schemas = SchemaManager::getList();
<div id="list-schemas">
    <?php
        foreach ($schemas as $elt) {
            echo '<input id="' . $elt->getSchema() . '" type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
        }
    ?>                          
</div>

Here is how to Update or Add a User in database by CREATING a new User:

    case "addUsers":
        {
            $p = new User(["idUser" => $_POST["id"], "login" => $_POST["pseudo"], "password" => $_POST["password"], "roles" => isset($_POST["roles"]) ? $_POST["roles"] : null, "schemas" => isset($_POST["schemas"]) ? $_POST["schemas"] : null]);
            UserManager::add($p);
            break;
        }
    case "updUsers":
        {
            $p = new User(["idUser" => $_POST["id"], "login" => $_POST["pseudo"], "password" => $_POST["password"], "roles" => isset($_POST["roles"]) ? $_POST["roles"] : null, "schemas" => isset($_POST["schemas"]) ? $_POST["schemas"] : null]);
            UserManager::update($p);
            break;
        }

It means that when add or update a User : roles and schemas are not empty, because the changes are done in database. However, I would like to see the details of each Users, so I made that :

$list = UserManager::getList();
var_dump($list);
foreach ($list as $elt) {
    $id=$elt->getIdUser();
    $pers = UserManager::getById($id);
    echo '<tr>';
    echo '<td><a href="#edit'.$elt->getIdUser().'">Details</a></td>';

    echo '<div class="lightbox" id="edit'.$elt->getIdUser().'">';
    echo '<figure>';
    echo '<a href="#" class="closemsg"></a>';
    echo '<figcaption><h4>'.$elt->getLogin().'</h4>';
    $roles=$pers->getRoles();
    var_dump($roles); // <-------------NULL
    if(isset($roles) && !empty($roles)){
        echo '<p>Membership of roles :</p>';
        echo '<ul>';
        foreach ($roles as $role) {
            echo '<li>'.$role.'</li>';
        }
        echo '</ul>';
    }
    $schemas=$pers->getSchemas();
    var_dump($schemas); // <-------------NULL
    if(isset($schemas) && !empty($schemas)){
        echo '<p>Grantee of schemas :</p>';
        echo '<ul>';
        foreach ($schemas as $schema) {
            echo '<li>'.$schema.'</li>';
        }
        echo '</ul>';
    }
    echo '</figcaption>';
    echo '</figure>';
    echo '</div>';

    echo '</tr>';
}

& when I click on "Details" nothing appears for roles & schemas. WHY ??? Here is for example, a User when we do var_dump($list); :

object(User)[10]
      private '_idUser' => string '24734' (length=5)
      private '_login' => string 'user10' (length=6)
      private '_motDePasse' => string '********' (length=8)
      private '_roles' => null
      private '_schemas' => null

Can anybody help me ?




Aucun commentaire:

Enregistrer un commentaire