I
I
Ivan Karabadzhak2013-12-17 11:20:23
Yii
Ivan Karabadzhak, 2013-12-17 11:20:23

Why doesn't beforeSafe work in the ActiveRecord model (Yii Framework)?

Most likely the reason is banal, but I do not know the answer.
beforeSafe itself

protected function beforeSafe()
    {
        if (preg_match('/(\d{2})\.(\d{2})\.(\d{4})/', $this->start))
        {
            $this->start = date('Y-m-d H:i:s', strtotime($this->start));
        }
        if (preg_match('/(\d{2})\.(\d{2})\.(\d{4})/', $this->finish))
        {
            $this->finish = date('Y-m-d H:i:s', strtotime($this->finish));
        }
        return parent::beforeSafe();
    }

Where does the model come from
$order_model = Order::model()->getById($id);
In the model itself
public function getById($id)
    {
        $user = Order::model()->find('id = :id', array(':id' => $id));
        return $user;
    }

When I
$order_model->save();
do BeforeSafe does not work. Checked in the following way. Did just inside it $this->title = 'TEST'; But the title field did not change. Therefore, changing the values ​​of the $this->start and $this->finish fields is out of the question.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Vasiliev, 2013-12-17
@Jakeroid

1) instead of beforeSafe - beforeSave
2) Why getById method when there is findByAttributes(array('id' => $id)) and findByPk($id)

A
Alexey Kupreichik, 2013-12-17
@plusodinminus

Most likely the reason is banal, but I do not know the answer.

You are right here :)
@bobahvas said everything correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question