tareq1988 / wp-eloquent

Eloquent ORM for WordPress
https://tareq.co/2015/05/eloquent-orm-in-wordpress/
566 stars 135 forks source link

SAVE NOT WORKING #12

Open Aneta1990 opened 8 years ago

Aneta1990 commented 8 years ago

READ WORK

SAVE NOT WORKING!!!!!!!!!!

Aneta1990 commented 8 years ago

I can't save any object.

Aneta1990 commented 8 years ago

Help please

perssonmattias91 commented 8 years ago

@Aneta1990 You have to provide some details on your issue. Do you get any error messages? What exactly are you trying to save?

al-v-in commented 7 years ago

Same issue. Reading via models works fine. Insert via DB facade works fine. But save via models doesnt work though returns true and gives no exceptions.

eclipsesrl commented 5 years ago

Guys think I've found the issue, it's a mismatch between ID and id of the model, practically even if you have set the primarykey and the guarded to ID, the model returns id in lower case, so before saving updating or deleting you have to set the property ID to the one saved in id

`use WeDevs\ORM\Eloquent\Model;

class ClassBaseModel extends Model {

public function save(array $options = []) {
    if ($this->id) {
        $this->ID = $this->id;
    }
    $saved = parent::save($options);
    $error = $this->getConnection()->db->last_error;

    if ($error) {
        throw new \Exception($error);
    }
    return $saved;
}

public function delete()
{
    if ($this->id) {
        $this->ID = $this->id;
    }
    $deleted = parent::delete();
    $error = $this->getConnection()->db->last_error;

    if ($error) {
        throw new \Exception($error);
    }
    return $deleted;
}

public function update(array $attributes = [], array $options = [])
{
    if ($this->id) {
        $this->ID = $this->id;
    }
    $updated = parent::update($attributes, $options);
    $error = $this->getConnection()->db->last_error;

    if ($error) {
        throw new \Exception($error);
    }
    return $updated;

}

}`