本文整理汇总了PHP中app\models\Book::findOne方法的典型用法代码示例。如果您正苦于以下问题:PHP Book::findOne方法的具体用法?PHP Book::findOne怎么用?PHP Book::findOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Book
的用法示例。
在下文中一共展示了Book::findOne方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionForm
public function actionForm($book_id)
{
$product = Book::findOne($book_id);
$productImage = new BookImage();
if (!empty($_FILES['BookImage'])) {
$img = $_FILES['BookImage']['name']['url'];
$ext = end(explode(".", $img));
$name = microtime();
$name = str_replace(' ', '', $name);
$name = str_replace('.', '', $name);
$name = $name . '.' . $ext;
$tmp = $_FILES['BookImage']['tmp_name']['url'];
$productImage->url = $name;
move_uploaded_file($tmp, '../uploads/' . $name);
}
if (!empty($_POST)) {
$productImage->name = $_POST['BookImage']['name'];
$productImage->product_id = $book_id;
if ($productImage->save()) {
$session = new Session();
$session->open();
$session->setFlash('message', 'Data Saved.');
return $this->redirect(['index', 'book_id' => $book_id]);
}
}
return $this->render('//BookImage/Form', ['product' => $product, 'productImage' => $productImage]);
}
示例2: loadModel
/**
* @param $id
* @return null|Book
* @throws NotFoundHttpException
*/
private function loadModel($id)
{
$model = Book::findOne($id);
if (null == $model) {
throw new NotFoundHttpException('Книга не найдена');
}
return $model;
}
示例3: actionDelete
public function actionDelete($id)
{
$book = Book::findOne(['id' => $id]);
if ($book) {
$book->delete();
}
$this->redirect(Url::to(['books/index']));
}
示例4: findModel
protected function findModel($id)
{
if (($model = Book::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
示例5: actionDelete
public function actionDelete()
{
$id = (int) Yii::$app->request->get('id');
/** @var Book $book */
$book = Book::findOne(['id' => $id]);
$result = $book ? $book->delete() : false;
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ['result' => (bool) $result];
}
示例6: update
public function update()
{
$this->preview = UploadedFile::getInstance($this, 'preview');
if ($this->validate()) {
$book = Book::findOne($this->book_id);
$book->name = $this->name;
$book->date = strtotime($this->date);
$book->author_id = $this->author_id;
$dir = Yii::getAlias(Yii::$app->params['previewPath']);
$uploaded = false;
if ($this->preview) {
$filename = 'b' . $this->book_id . 'preview.' . $this->preview->extension;
$uploaded = $this->preview->saveAs($dir . '/' . $filename);
$img = Image::getImagine()->open($dir . '/' . $filename);
$size = $img->getSize();
$ratio = $size->getWidth() / $size->getHeight();
$width = 700;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/' . $filename, ['quality' => 80]);
$ratio = $size->getWidth() / $size->getHeight();
$width = 200;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/th_' . $filename, ['quality' => 80]);
} else {
$filename = '';
}
if ($uploaded) {
if (is_file(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview)) {
unlink(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview);
}
$book->preview = $filename;
}
$book->save();
return $book;
}
return null;
}
示例7: actionViewAjax
/**
* View book.
* @return mixed
*/
public function actionViewAjax($id)
{
$this->layout = '/empty';
$model = Book::findOne($id);
return $this->render('view-ajax', ['model' => $model]);
}
示例8: update
/**
* update book.
* @return Book|null the saved model or null if saving fails
*/
public function update()
{
// подтягиваем файлы перед валидацией, что бы все вместе проверять
$this->preview = UploadedFile::getInstance($this, 'preview');
if ($this->validate()) {
$book = Book::findOne($this->book_id);
$book->name = $this->name;
$book->date = strtotime($this->date);
$book->author_id = $this->author_id;
// Дописываем файл
$dir = Yii::getAlias(Yii::$app->params['previewPath']);
$uploaded = false;
if ($this->preview) {
// если файл залился - пишем в базу данные по файлу
$filename = 'b' . $this->book_id . 'preview.' . $this->preview->extension;
$uploaded = $this->preview->saveAs($dir . '/' . $filename);
// урезаем размер файла, что б не грущили 100500мегабайт и пересохраняем
$img = Image::getImagine()->open($dir . '/' . $filename);
$size = $img->getSize();
$ratio = $size->getWidth() / $size->getHeight();
$width = 700;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/' . $filename, ['quality' => 80]);
// делаем превьюшку
$ratio = $size->getWidth() / $size->getHeight();
$width = 200;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/th_' . $filename, ['quality' => 80]);
} else {
$filename = '';
}
if ($uploaded) {
// удалим старый файл, если он был
if (is_file(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview)) {
unlink(Yii::getAlias(Yii::$app->params['previewPath']) . '/' . $book->preview);
}
$book->preview = $filename;
// обновляем имя файла аватара
}
$book->save();
return $book;
}
return null;
}
示例9: actionDelete
public function actionDelete($id)
{
$product = Book::findOne($id);
if (!empty($product)) {
if (!empty($product->img)) {
unlink('../uploads/' . $product->img);
}
$product->delete();
$session = new Session();
$session->open();
$session->setFlash('message', 'Data Deleted.');
return $this->redirect(['index']);
}
}
示例10: actionDelete
/**
* Удаление книги.
*
* @param int $id
* @return \yii\web\Response
*/
public function actionDelete($id)
{
/** @var Book $modelBook */
$modelBook = Book::findOne($id);
if ($modelBook->delete()) {
return $this->redirect(['back-book/index']);
}
return $this->refresh();
}
示例11: actionUpdate
public function actionUpdate($id = NULL)
{
if ($id === NULL) {
throw new HttpException(404, 'Not Found');
}
$model = Book::findOne($id);
if ($model === NULL) {
throw new HttpException(404, 'Document Does Not Exist');
}
if (isset($_POST['Book'])) {
$model->name = $_POST['Book']['name'];
$model->date_create = $_POST['Book']['date_create'];
$model->date_update = $_POST['Book']['date_update'];
$model->preview = $_POST['Book']['preview'];
$model->date = $_POST['Book']['date'];
$model->author_id = $_POST['Book']['author_id'];
if ($model->save()) {
return $this->redirect(['site/index']);
}
}
echo $this->render('create', array('model' => $model));
}
示例12: actionView
/**
* Страница просмотра книги по ее уникальному псевдниму.
*
* @param string $alias
* @return string
*/
public function actionView($alias)
{
$modelBook = Book::findOne(['alias' => $alias]);
return $this->render('view', ['modelBook' => $modelBook]);
}
示例13: getReservationFieldsProvider
protected function getReservationFieldsProvider($id)
{
$book = \app\models\Book::findOne($id);
$tourFields = \app\models\TourField::find()->where(['tour_id' => $book->tour_id])->orderBy('position')->all();
$bookValues = \app\models\BookValue::find()->where(['book_id' => $book->id])->indexBy('tour_field_id')->all();
//print '<pre>'; print_r($tourFields); die();
foreach ($tourFields as $field) {
$data[] = ['name' => $field->name, 'value' => array_key_exists($field->id, $bookValues) ? $bookValues[$field->id]->value : ''];
}
//print '<pre>'; print_r($data); die();
$provider = new \yii\data\ArrayDataProvider(['allModels' => $data]);
return $provider;
}
示例14: actionDelete
public function actionDelete($id)
{
Book::findOne($id)->delete();
return $this->goBack();
}