P
P
Pavel Khorikov2015-04-27 17:10:50
Yii
Pavel Khorikov, 2015-04-27 17:10:50

Properly saving models with linked Yii2 data?

There are two tables
1. Atricles [articles]
2. Authors [authors]
3. Articles_Authors [article_id author_id]
There is a many to many relationship between them with ids from the first table and the second.
What are the practices (without third-party extensions) for saving links in table 3.
How to implement saving data, taking into account the fact that I get data from an XML file when parsing?
At the moment, the import of articles is implemented:

foreach ($fileXML->issue->articles->article as $articleFromXML) {
                    $articleForImport = new Articles();
                    $articleForImport->ydk_code = strval($articleFromXML->codes->udk);
                    $articleForImport->header = strval($articleFromXML->artTitles->artTitle);
                    $articleForImport->annotation = strval($articleFromXML->abstracts->abstract);

                    foreach ($articleFromXML->keywords->kwdGroup as $keyWords) {
                        $forImplode = (array)$keyWords->keyword;
                        $keyWordsForImport = implode(', ', $forImplode);
                        $articleForImport->keywords = $keyWordsForImport;
                    }
                    $articleForImport->fk_issue = $issue->id;
                    if ($articleForImport->validate()) {
                        $articleForImport->save();
                    }

And import authors:
foreach ($articleFromXML->authors->author as $authorFromXML) {
                        $authorForImport = new Authors();
                        $authorForImport->surname = (string)$authorFromXML->individInfo->surname;
                        $authorInitials = explode(" ", $authorFromXML->individInfo->initials);
                        $authorForImport->name = $authorInitials[0];
                        $authorForImport->patronymic = $authorInitials[1];
                        $authorForImport->orgName = (string)$authorFromXML->individInfo->orgName;
                        $authorForImport->contactInfo = (string)$authorFromXML->individInfo->email;
                        $authorForImport->additionalInfo = (string)$authorFromXML->individInfo->otherInfo;

                        if ($authorForImport->validate()) {
                            $authorForImport->save();
                        }
                    }

The hardest part is understanding how models work for persisting related data in Yii2.
I tried to implement something like this (ArticlesAuthors model generated in Gii from the ArticlesAuthors table, similar to the Articles and Authors tables),
after getting all the authors and articles from XML, I try to create the ArticlesAuthors object:
$ArticlesAuthors = new ArticlesAuthors 
 ....
ArticlesAuthors->save();

Everything would be fine if each article had one author, everything would work, but when trying to save the article and authors, the option above saves only the first author of the first article (although the first article has three authors, for example).
How is this best implemented in Yii2?
And further. All import code is wrapped in a transaction. Whether it is necessary to use, say, when saving only authors to the Authors table, the save() method or the $transaction->commit() method; declared after import of all data means and so data saving?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2015-04-27
@Horik_off

The hardest part is understanding how models work for persisting related data in Yii2.

Everything just works there. If you understand how connections work. There are: one2one, one2many and many2many. The first two types are simple, when entity A contains a link to entity B. To organize the last type, you need a reference table, which you mention, it contains relationships between entities A and B.
In your version, as far as I understand, you can create all relationships manually :
$article = new Article( [ ... ] );
$article->save();

$author = new Author( [ ... ] );
$author->save();

$article2Author = new ArticleAuthor( [
    'article_id' => $article->id,
    'author_id' => $author->id,
] );
$article2Author->save();

G
gaparchi, 2015-08-28
@gaparchi

The link() method can be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question