S
S
skyzysky2021-12-22 23:55:46
MySQL
skyzysky, 2021-12-22 23:55:46

Why are records duplicated in the database (ActiveRecord, Yii2)?

Greetings. I have been looking at these 6 lines for a couple of hours now. That's all there is in the action. When executed, 2 identical records are created in the database with different id (auto-increment).

Here is the action code:

public function actionIndex()
    {
        $draftOrder = new OrdersDraft();
        $draftOrder->castomer_id = 22;
        $draftOrder->title = "Черновик заказа " . date("d.m.y H:i:s", time());
        $draftOrder->status_id = 1;
        $draftOrder->date_add = time();
        $draftOrder->save();
    }

And the model code:
namespace common\models;

use Yii;

class OrdersDraft extends \yii\db\ActiveRecord
{
    
    
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'orders';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['castomer_id', 'title', 'status_id', 'date_add'], 'required'],
            [['castomer_id', 'status_id', 'date_add'], 'integer'],
            [['title'], 'string', 'max' => 150],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'castomer_id' => 'Castomer ID',
            'city' => 'Город',
            'category_id' => 'Category ID',
            'title' => 'Заголовок',
            'price_min' => 'Мин. цена',
            'price_max' => 'Макс. цена',
            'description' => 'Описание',
            'delivery' => 'Доставка',
            'deadline' => 'Актуально до',
            'status_id' => 'Статус',
            'date_add' => 'Добавлен',
        ];
    }
}


I would really appreciate any ideas.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander Makarov, 2021-12-23
@SamDark

There is no problem in this code.

S
skyzysky, 2021-12-23
@skyzysky

Perhaps the question should be put like this: how can I make yii create an entry in the table twice, given the code that I have given? Could it be something outside of the controller and model?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question