本文整理汇总了PHP中app\models\Books::findOne方法的典型用法代码示例。如果您正苦于以下问题:PHP Books::findOne方法的具体用法?PHP Books::findOne怎么用?PHP Books::findOne使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Books
的用法示例。
在下文中一共展示了Books::findOne方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
public function save()
{
$model = Books::findOne($this->id);
$fileInstance = UploadedFile::getInstance($this, self::FIELD_PREVIEW);
if ($fileInstance) {
$filePath = 'images/' . $fileInstance->baseName . '.' . $fileInstance->extension;
$model->preview = $fileInstance->baseName . '.' . $fileInstance->extension;
$fileInstance->saveAs($filePath, false);
$imagine = new Gd\Imagine();
try {
$filePath = 'images/preview_' . $fileInstance->baseName . '.' . $fileInstance->extension;
$img = $imagine->open($fileInstance->tempName);
$size = $img->getSize();
if ($size->getHeight() > $this->imageSizeLimit['width'] || $size->getWidth() > $this->imageSizeLimit['height']) {
$img->resize(new Box($this->imageSizeLimit['width'], $this->imageSizeLimit['height']));
}
$img->save($filePath);
//
} catch (\Imagine\Exception\RuntimeException $ex) {
$this->addError(self::FIELD_PREVIEW, 'Imagine runtime exception: ' . $ex->getMessage());
return FALSE;
}
}
if (!$this->validate()) {
return false;
}
$model->name = $this->name;
$model->author_id = $this->author_id;
$model->date = DateTime::createFromFormat('d/m/Y', $this->date)->format('Y-m-d');
$model->date_update = new Expression('NOW()');
$model->save();
return true;
}
示例2: findModel
protected function findModel($id)
{
if (($model = Books::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested book not exist.');
}
}
示例3: loadModel
private function loadModel($id)
{
$model = Books::findOne($id);
if ($model == NULL) {
throw new HttpException(404, 'Model not found.');
}
return $model;
}
示例4: actionUpload
public function actionUpload($id)
{
if (($model = Books::findOne($id)) !== null) {
$file = UploadedFile::getInstanceByName('file');
if ($file) {
$file->saveAs($model->getCoverPath());
} else {
throw new NotFoundHttpException('Failed to load the file.');
}
} else {
throw new NotFoundHttpException('The book does not exist.');
}
}
示例5: actionClose
/**
* Updates a new Rents model with closing info.
* @return mixed
*/
public function actionClose()
{
if (isset($_GET['book_id'])) {
if ($model = Rents::find()->where(['book_id' => $_GET['book_id'], 'user_id' => Yii::$app->user->id, 'status' => 'rented'])->one()) {
$model->prev_date = date('Y-m-d', time());
$model->status = 'closed';
$model->save();
$book = Books::findOne($model->book_id)->updateStatus('available');
}
return $this->redirect(['book/index']);
} else {
return $this->redirect(['book/index']);
}
}
示例6: test_Delete_Warning_FileWasDeletedWithSyncON
public function test_Delete_Warning_FileWasDeletedWithSyncON()
{
$log_filename = $this->initAppFileSystem() . '/runtime/logs/logs.txt';
$this->mockYiiApplication(['bootstrap' => ['log'], 'components' => ['log' => ['traceLevel' => 0, 'targets' => ['generic' => ['class' => \yii\log\FileTarget::class, 'logVars' => [], 'logFile' => $log_filename, 'enabled' => true, 'levels' => ['warning']]]]]]);
$book_delete = $this->books['expected'][0];
\Yii::$app->mycfg->library->sync = true;
$book = Books::findOne(['book_guid' => $book_delete['book_guid']]);
$book->delete();
\Yii::getLogger()->flush(true);
$this->assertRegExp('/filename\\-1\' was removed before record deletion with sync enabled$/', file_get_contents($log_filename));
}
示例7: findModel
/**
* Finds the Books model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Books the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Books::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('Запрашиваемая странца не существует.');
}
}
示例8: test_action_Manage_Edit
/**
* @dataProvider pSync
*
* ACTION MUST:
*
* 1. not allow changes of book_guid, created and updated date, filename
* 2. generate filename based on title
* 3. generate updated_date
* 4. rename file if sync is ON
*/
public function test_action_Manage_Edit($sync)
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$book = $this->books['insert'][0];
$book_expected = $this->books['expected'][0];
$filename_expected = $filename_old = \Yii::$app->mycfg->library->directory . $book_expected['filename'];
file_put_contents($filename_expected, 'sample-data');
\Yii::$app->mycfg->library->sync = $sync;
$_POST['oper'] = 'edit';
$_POST['id'] = $book['book_guid'];
// CHANGING
// [ 1 ]
$_POST['created_date'] = '2000-01-01';
$_POST['updated_date'] = '2000-01-01';
$_POST['filename'] = '2000-01-01';
// [ 2 ]
$book_expected['filename'] = ", ''title book #1'', [].";
// [ 3 ]
$book_expected['updated_date'] = (new \DateTime())->format('Y-m-d H:i:s');
$this->controllerSite->runAction('manage');
/* @var $book_current \yii\db\BaseActiveRecord */
$book_current = Books::findOne(['book_guid' => $book['book_guid']]);
//remove seconds, as it fails on slow machines, definely fails on Travis
$book_expected['updated_date'] = (new \DateTime($book_expected['updated_date']))->format('Y-m-d H:i');
$book_current['updated_date'] = (new \DateTime($book_current['updated_date']))->format('Y-m-d H:i');
//var_dump($book_expected,$book_current->getAttributes()); die;
$this->assertArraySubset($book_expected, $book_current->getAttributes());
if ($sync) {
$filename_expected = \Yii::$app->mycfg->library->directory . $book_expected['filename'];
$this->assertFileNotExists($filename_old);
}
$this->assertFileExists($filename_expected);
$this->assertEquals(file_get_contents($filename_expected), 'sample-data');
}
示例9: test_action_Manage_Edit
/**
* @dataProvider pSync
*
* ACTION MUST:
*
* 1. not allow changes of book_guid, created and updated date, filename
* 2. generate filename based on title
* 3. generate updated_date
* 4. rename file if sync is ON
*/
public function test_action_Manage_Edit($sync)
{
// CONFIGURE
$book = $this->books['inserted'][0];
$book_expected = $this->books['expected'][0];
$filename_expected = $filename_old = \Yii::$app->mycfg->library->directory . $book['filename'];
file_put_contents($filename_expected, 'sample-data');
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['oper'] = 'edit';
$_POST['id'] = $book['book_guid'];
$_POST['created_date'] = '2000-01-01';
$_POST['updated_date'] = '2000-01-01';
$_POST['filename'] = '2000-01-01';
\Yii::$app->mycfg->library->sync = $sync;
// - - - - - -
$this->controllerSite->runAction('manage');
$book_expected['filename'] = ", ''title book #1'', [].";
// #1
// WORKAROUND FOR TRAVIS
$dt = new \DateTime();
$dt->setTimezone(new \DateTimeZone(\Yii::$app->getTimeZone()));
$book_expected['updated_date'] = $dt->format('Y-m-d H:i:s');
//CHECKING
/* @var $book_current \yii\db\BaseActiveRecord */
$book_current = Books::findOne(['book_guid' => $book['book_guid']]);
// #2
// WORKAROUND FOR TRAVIS: remove seconds, as it fails on slow machines, definely fails on Travis
$book_expected['updated_date'] = (new \DateTime($book_expected['updated_date']))->format('Y-m-d H:i');
$book_current['updated_date'] = (new \DateTime($book_current['updated_date']))->format('Y-m-d H:i');
// #3
$book_current_arr = $book_current->getAttributes();
$keys = array_keys($book_expected);
foreach ($keys as $k) {
if ($k == 'filename') {
// skip filename checks here. checked at #4 below
continue;
}
$this->assertEquals($book_expected[$k], $book_current_arr[$k], "expected '{$k}' doesn't match");
}
// #4
if ($sync) {
// file rename if sync ON
$filename_expected = \Yii::$app->mycfg->library->directory . $book_expected['filename'];
// renamed new
$this->assertFileNotExists($filename_old);
// old is not existed
}
$this->assertFileExists($filename_expected);
$this->assertEquals(file_get_contents($filename_expected), 'sample-data');
}
示例10: update
private function update($id, $attributes)
{
/* @var $book Books */
$book = Books::findOne(['book_guid' => $id]);
$book->scenario = 'edit';
$book->attributes = $attributes;
if (!$book->save()) {
throw new \yii\web\BadRequestHttpException(print_r($book->getErrors(), true));
}
}
示例11: actionViewBook
public function actionViewBook()
{
if (Yii::$app->user->isGuest) {
throw new \yii\web\HttpException(404);
}
if (!($model = Books::findOne([Books::FIELD_ID => Yii::$app->request->get(Books::FIELD_ID)]))) {
return false;
}
return $this->renderPartial('_book_view_modal', ['model' => $model]);
}