T
T
tigra2015-09-28 17:23:48
Yii
tigra, 2015-09-28 17:23:48

How to update a record by ID in a form?

There is a form that adds a record.
We have an id field. pk to db.
It is necessary that if a key is entered in the field that already exists, then the data is updated, if not, then a new pk is added, respectively

if (isset($_POST[get_class($model)]))
      {
       $model->attributes = $_POST[get_class($model)];
       if ($model->validate())
       {
        $event = new Calendar();

        $event->id = $model->id;
        $event->id_name = $model->id_name;
        $event->address = $model->address;
        $event->date = $model->date;
        $event->company = $model->company;
        $event->timestart = $model->timestart;
        $event->timeend = $model->timeend;
        $event->other_info = $model->other_info;

        $event->save();
       }
}

How to write it in an action?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tigra, 2015-09-29
@tigroid3

if ($model->id) {
    $event = Calendar::model()->find($model->id);
} else {
    $event = new Calendar();
}

$event->id_name = $model->id_name;
$event->address = $model->address;
...

$event->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question