本文整理汇总了PHP中app\models\Book::link方法的典型用法代码示例。如果您正苦于以下问题:PHP Book::link方法的具体用法?PHP Book::link怎么用?PHP Book::link使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Book
的用法示例。
在下文中一共展示了Book::link方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: runBookInsertion
function runBookInsertion($i)
{
$book = new Book();
$book->title = 'Hello' . $i;
$book->isbn = '1234';
$book->price = $i;
//$book->author_id = $this->authors[array_rand($this->authors)]->id;
$book->link('author', $this->authors[array_rand($this->authors)]);
$book->save(false);
$this->books[] = $book;
}
示例2: _saveBook
/**
* Сохраняет модель и связанные данные
*
* @param Book $modelBook
* @return \yii\web\Response
*/
private function _saveBook(Book $modelBook)
{
$authorIds = Yii::$app->request->post('authors');
$tagIds = Yii::$app->request->post('tags');
if ($modelBook->load(Yii::$app->request->post()) && $modelBook->save()) {
array_map(function ($model) use($modelBook) {
$modelBook->unlink('authors', $model, true);
}, $modelBook->authors);
foreach ($authorIds ?: [] as $authorId) {
/** @var Author $modelAuthor */
$modelAuthor = Author::findOne($authorId);
$modelBook->link('authors', $modelAuthor);
}
array_map(function ($model) use($modelBook) {
$modelBook->unlink('tags', $model, true);
}, $modelBook->tags);
foreach ($tagIds ?: [] as $tagId) {
/** @var Tag $modelTag */
$modelTag = Tag::findOne($tagId);
$modelBook->link('tags', $modelTag);
}
return $this->redirect(['back-book/view', 'id' => $modelBook->id]);
}
}