本文整理匯總了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]);
}
}