Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evitar que el update ponga NULL a campos no pasados. #72

Open
henrystivens opened this issue Mar 14, 2016 · 2 comments
Open

Evitar que el update ponga NULL a campos no pasados. #72

henrystivens opened this issue Mar 14, 2016 · 2 comments

Comments

@henrystivens
Copy link
Member

La función QueryGenerator::update genera la cadena UPDATE con todos los nombres de los campos que saca de la tabla y esto hace que si el campo no está definido en el objeto(LiteRecord) que se le está pasando lo ponga como NULL en la cadena borrando cualquier dato que haya registrado en la base de datos.

@henrystivens
Copy link
Member Author

Hola,

Con esto funciona pero no se si rompe algo que ya tengan contemplado y yo esté pasando por alto.

    public static function update(\Kumbia\ActiveRecord\LiteRecord $model, array &$data)
    {
        $set = [];
        $pk = $model::getPK();
        /*elimina la clave primaria*/
        $list = array_diff($model::metadata()->getFieldsList(), [$pk]);
        foreach ($list as $field) {
            if(isset($model->$field)){
                static::updateField($field, $model->$field, $data, $set, $model);
            }
        }
        $set = \implode(', ', $set);
        $source = $model::getSource();
        $data[":$pk"] = $model->$pk;
        return "UPDATE $source SET $set WHERE $pk = :$pk";
    }

@bethuxs
Copy link
Member

bethuxs commented Oct 24, 2020

Esto se acomodó?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants