本文整理汇总了PHP中app\models\Books类的典型用法代码示例。如果您正苦于以下问题:PHP Books类的具体用法?PHP Books怎么用?PHP Books使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Books类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: search
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Books::find()->with('authors');
$dataProvider = new ActiveDataProvider(['query' => $query]);
$dataProvider->setSort(['attributes' => ['id', 'name', 'authorFullName' => ['asc' => ['authors.firstname' => SORT_ASC, 'authors.lastname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC, 'authors.lastname' => SORT_DESC], 'label' => 'Автор'], 'date_update', 'date_create', 'date']]);
$this->load($params);
// валидация
if (!$this->validate()) {
$query->joinWith(['authors']);
return $dataProvider;
}
// Фильтр по Автору
$query->joinWith(['authors' => function ($q) {
$this->authorFullName = trim($this->authorFullName);
if ($this->authorFullName) {
$Asearch = str_replace(" ", "|", $this->authorFullName);
$q->where('`authors`.`firstname` RLIKE "' . $Asearch . '" ' . 'OR `authors`.`lastname` RLIKE "' . $Asearch . '"');
}
}]);
/* $query->andFilterWhere([
'name' => $this->name,
'authors.firstname' => $this->authorFullName,
]);*/
$query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['BETWEEN', 'date', $this->date_1, $this->date_2]);
return $dataProvider;
}
示例2: search
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Books::find();
$dataProvider = new ActiveDataProvider(['query' => $query]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if ($this->author_id) {
$query->andFilterWhere(['author_id' => $this->author_id]);
}
if ($this->date_from) {
if ($this->date_till) {
$query->andFilterWhere(['date' => ['between', $this->date_from, $this->date_till]]);
} else {
$query->andFilterWhere(['>=', 'date', $this->date_from]);
}
} elseif ($this->date_till) {
$query->andFilterWhere(['<=', 'date', $this->date_till]);
}
if ($this->name) {
$query->andFilterWhere(['like', 'name', $this->name]);
}
return $dataProvider;
}
示例3: 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;
}
示例4: search
public function search($params)
{
Yii::$app->session['BooksSearch'] = $params;
$query = Books::find();
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 5]]);
$dataProvider->setSort(['attributes' => ['id', 'name', 'preview', 'authorfullname' => ['asc' => ['authors.firstname' => SORT_ASC, 'authors.lasttname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC, 'authors.lasttname' => SORT_DESC], 'label' => Yii::t('app', 'Author'), 'default' => SORT_ASC], 'date', 'date_create']]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
$query->joinWith(['author']);
return $dataProvider;
}
$query->andFilterWhere(['like', 'name', $this->name]);
if ($this->date_from) {
$query->andFilterWhere(['>=', 'date', $this->date_from]);
}
if ($this->date_to) {
$query->andFilterWhere(['<=', 'date', $this->date_to]);
}
if ($this->author_id) {
$query->andFilterWhere(['author_id' => $this->author_id]);
}
$query->joinWith(['author']);
return $dataProvider;
}
示例5: getSearchQuery
/**
* Get query for book search
* @return [QueryInterface]
*/
public function getSearchQuery()
{
$query = Books::find();
//Author
$query = $query->filterWhere(['author_id' => $this->author_id]);
//Name
if (!empty($this->name)) {
$query = $query->andWhere(['like', 'name', $this->name]);
}
//Date
if ($this->firstTimestamp != 0 && $this->secondTimestamp != 0) {
if ($this->firstTimestamp == $this->secondTimestamp) {
$query = $query->andWhere(['=', 'date', $this->firstTimestamp]);
} else {
$query = $query->andWhere(['between', 'date', $this->firstTimestamp, $this->secondTimestamp]);
}
} else {
if ($this->firstTimestamp != 0) {
$query = $query->andWhere(['>=', 'date', $this->firstTimestamp]);
} else {
if ($this->secondTimestamp != 0) {
$query = $query->andWhere(['<=', 'date', $this->secondTimestamp]);
}
}
}
return $query;
}
示例6: findModel
protected function findModel($id)
{
if (($model = Books::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested book not exist.');
}
}
示例7: loadModel
private function loadModel($id)
{
$model = Books::findOne($id);
if ($model == NULL) {
throw new HttpException(404, 'Model not found.');
}
return $model;
}
示例8: index
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$keyword = Input::get('keyword');
if (!$keyword) {
$books = Books::orderBy('title', 'ASC')->get();
} else {
$books = Books::where('title', 'like', '%' . $keyword . '%')->orWhere('author', 'like', '%' . $keyword . '%')->get();
}
return view('catalog.index', compact('books'))->with('result', 'OK');
}
示例9: actionRemove
public function actionRemove($id)
{
if (Yii::$app->request->isAjax) {
$model = Books::find()->where(['id' => $id])->one();
$model->delete();
Yii::$app->response->format = Response::FORMAT_JSON;
return array('result' => true);
}
return $this->redirect('index.php?r=site%2Fbooks');
}
示例10: getAuthorBooks
public static function getAuthorBooks($author_id, $return_count = true)
{
$author_id = (int) $author_id;
$data = Books::find()->where('authors LIKE "%,' . $author_id . '" OR authors LIKE "%' . $author_id . '," OR authors LIKE "' . $author_id . '"');
if ($return_count) {
$data = (int) $data->count();
} else {
$data = $data->all();
}
return $data;
}
示例11: search
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Books::find()->addSelect([$this->tableName() . ".*", "CONCAT(authors.firstname, ' ', authors.lastname) AS author_fullname"])->joinWith(['author']);
$dataProvider = new ActiveDataProvider(['query' => $query]);
$dataProvider->sort->attributes['author'] = ['asc' => ['authors.firstname' => SORT_ASC], 'desc' => ['authors.firstname' => SORT_DESC]];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// поиск по названию книги
$query->andFilterWhere(['like', 'books.name', $this->name]);
// поиск по дате от/до
if (!empty($this->book_date_from) and !empty($this->book_date_to)) {
// конвертирую в корректный формат времени и добавляю четкие временные границы
$book_date_from = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->book_date_from), 'dd-MM-yyyy 00:00:00');
$book_date_to = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->book_date_to), 'dd-MM-yyyy 23:59:59');
$book_date_from = Yii::$app->formatter->asTimestamp($book_date_from);
$book_date_to = Yii::$app->formatter->asTimestamp($book_date_to);
$query->andFilterWhere(['between', 'books.date', $book_date_from, $book_date_to]);
}
// поиск по автору
if ($this->author_fullname == 7 or in_array(trim(mb_strtolower($this->author)), array('без', 'без привязки', 'без привязки к автору'))) {
// для книг без привязки к автору - из books
$query->andFilterWhere(['=', 'author_id', 0]);
} else {
// из authors
$query->andFilterWhere(['like', 'authors.firstname', $this->author]);
$query->andFilterWhere(['=', 'authors.id', $this->author_fullname]);
}
/////////////////////// доп параметры для filterModel (если раскомменчены filterModel и Pjax в books/index.php)
$query->andFilterWhere(['=', 'books.id', $this->id])->andFilterWhere(['like', 'books.preview', $this->preview]);
// дата Выхода книги
if (!empty($this->date)) {
// конвертирую в корректный формат времени и добавляю четкие временные границы
$book_date_from = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->date), 'dd-MM-yyyy 00:00:00');
$book_date_to = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->date), 'dd-MM-yyyy 23:59:59');
$book_date_from = Yii::$app->formatter->asTimestamp($book_date_from);
$book_date_to = Yii::$app->formatter->asTimestamp($book_date_to);
$query->andFilterWhere(['between', 'books.date', $book_date_from, $book_date_to]);
}
// дата Добавления книги
if (!empty($this->date_create)) {
// конвертирую в корректный формат времени и добавляю четкие временные границы
$book_date_from = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->date_create), 'dd-MM-yyyy 00:00:00');
$book_date_to = Yii::$app->formatter->asDatetime(str_replace("/", "-", $this->date_create), 'dd-MM-yyyy 23:59:59');
$book_date_from = Yii::$app->formatter->asTimestamp($book_date_from);
$book_date_to = Yii::$app->formatter->asTimestamp($book_date_to);
$query->andFilterWhere(['between', 'books.date_create', $book_date_from, $book_date_to]);
}
///////////////////
return $dataProvider;
}
示例12: search
public function search($params)
{
$query = Books::find();
$dataProvider = new ActiveDataProvider(['query' => $query]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['author_id' => $this->author_id]);
$query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['<=', 'date', $this->date_to])->andFilterWhere(['>=', 'date', $this->date_from]);
return $dataProvider;
}
示例13: dashboard
public function dashboard()
{
// Check Author authentication Session
if (!Auth::check('default')) {
return $this->redirect('Authors::login');
}
// Retrieve current Author ID
$author_id = Auth::check('default')->data['id'];
// Retrieve Books from the current Author
$conditions = array('Books.author_id' => Auth::check('default')->data['id']);
$books = Books::find('all', compact('conditions'));
return compact('books', 'author_id');
}
示例14: 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.');
}
}
示例15: 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']);
}
}