S
S
Sergey Beloventsev2016-05-04 12:50:59
Yii
Sergey Beloventsev, 2016-05-04 12:50:59

How to save to database?

I write a parser that saves data from a third-party site at the entered page address,
I get an array of this kind

array(1){
    ['category']=>"lorem ipsum"
    ['description']=>'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'
    ['page']=>'За словесными горами ';
    ['desc_page']=>'Далеко-далеко за словесными горами в стране гласных и согласных живут рыбные тексты. Вдали от всех живут они в буквенных домах на берегу Семантика большого языкового океана.'
    };

well, since the categories can be repeated on different pages, the question is how can I properly save to the database in case there is no category with the name "lorem ipsum" in the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-05-04
@Sergalas

First look for the category on the title. If you did not find such a category, then add it to the database. If found, then use it further in the code.
Something like this:

$category = Category::find()->where(['title' => $title])->one();

if($category === null) {
    $category = new Category();
    // Заполняем данными
    // ...
    $category->save();
}

echo $category->title;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question