Make sql update with PHP object

0

I have a model object named student:

    class Aluno
{

    //ESTA CLASSE TEM Q SER IGUAL AO BANCO DE DADOS
    public $nome;
    public $sobrenome;
    public $email;
    public $senha;
    public $unidadelocal;
    public $unidadecurricular;
    public $diapreparacao;
    public $liberadoexercicio;

    public function __get($var)
    {
        throw new Exception("Invalid property $var");
    }

    public function __set($var, $value)
    {
        $this->__get($var);
    }
}

And I need to do an update on the database with all the data that is in this object, however I want to do it in an "automatic" way, without having to do it manually adding all the variables in the query.

What I tried was something with foreach but without success:

public function atualizar($aluno)
{
    //print_r(serialize($aluno));
    $sql = new Sql();

    $fieldlist = $this->fieldlist;
    foreach ($aluno as $field => $fieldvalue) {
        if (!in_array($field, $fieldlist)) {
            unset ($aluno[$field]);
        } // if
    } // foreach

    $where  = NULL;
    $update = NULL;
    foreach ($aluno as $item => $value) {
        if (isset($fieldlist[$item][''])) {
            $where .= "$item='$value' AND ";
        } else {
            $update .= "$item='$value', ";
        } // if
    } // foreach
    $where  = rtrim($where, ' AND ');
    $update = rtrim($update, ', ');
    $query = "UPDATE tb_alunos SET $update WHERE $where";

    return $sql->query($query,$aluno);

}
    
asked by anonymous 02.02.2018 / 12:17

0 answers