I am getting model object 3 times (Yii2) to load view controller. This makes my page to load slow. How to reduce it?
public function behaviors()
{
return [
'httpCache' => [
'class' => 'yii\filters\HttpCache',
'only' => ['view'],
'lastModified' => function ($action, $params) {
$post = $this->findModel(Yii::$app->request->get('id'));
return strtotime($post->updated);
},
'etagSeed' => function ($action, $params) {
$post = $this->findModel(Yii::$app->request->get('id'));
return serialize([$post->updated, $post->views, $post->comments, Yii::$app->user->isGuest ? 0 : 1]);
}
],
];
}
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model,
]);
}
You can cache model instance at controller level:
private $_models = [];
protected function findModel($id) {
if (!array_key_exists($id, $this->_models)) {
$this->_models[$id] = MyModel::findOne($id);
if ($this->_models[$id] === null) {
$this->notFound();
}
}
return $this->_models[$id];
}
Only first call of findModel() will query DB, next calls will return already instantiated object.
Related
I have an end API point
users/{user}
now in User resource, I want to return
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
// 'comments' => $this->post->comments->keyBy('post_id')
'comments' => new CommentCollection($this->post->comments->keyBy->post_id)
];
}
CommentCollection class
public function toArray($request)
{
// return parent::toArray($request);
return [
'data' => $this->collection->transform(function($comment){
return [
'id' => $comment->id,
'comment' => $comment->comment,
];
}),
];
}
but the result will not include the post_id as key, how I can make it return the comments collection having key post_id?
Update
use App\models\Post;
use App\Http\Resources\Postas PostResource;
Route::get('/posts', function () {
return PostResource::collection(Post::all()->keyBy->slug);
});
This is working correctly, but if I will use post collection inside User resource as relationship, it is not working! and that is my requirement in comments collection.
What I did it, I created another ResourceGroupCollection class
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentGroupCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Collection\CommentCollection';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Comment';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
and then
new CommentGroupCollection($comments->groupBy('post_id')),
just like this :
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
// 'comments' => $this->post->comments->keyBy('post_id')
'comments' => new CommentCollection($this->post->comments)->keyBy('post_id')
];
}
I made a Models controller and use it as resources for almost all my Model. Now, when I try to get the data of a model and the related models, Laravel replace upper case letter with an underscore and the lower case letter. I need to let it with the upper case.
So there is the model where I got the issue at App\Models\Rate:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Rate extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = 'rates';
protected $fillable = [
'institution_id',
'name',
];
protected $info = [
'relations' => [
'rateRistournes' => [
'model' => 'RateRistourne',
'type' => 'hasMany',
],
'rateRows' => [
'model' => 'RateRow',
'type' => 'hasMany',
],
'rateTables' => [
'model' => 'RateTable',
'type' => 'hasMany',
],
],
'rules' => [
],
'hashid' => false,
];
public function getRelations()
{
return $this->info['relations'];
}
public function getRules()
{
return $this->info['rules'];
}
public function useHashid()
{
return $this->info['hashid'];
}
public function institution()
{
return $this->belongsTo(Institution::class);
}
public function rateTables()
{
return $this->hasMany(RateTable::class);
}
public function rateRows()
{
return $this->hasMany(RateRow::class);
}
public function rateRistournes()
{
return $this->hasMany(RateRistourne::class);
}
}
And this is the function that contain the query into ModelsController:
public function show($name, $id)
{
$data = $this->retrieveModelAndRelations($name, $id);
if (is_null($data)) {
return $this->sendError('Model not found.');
}
return $this->sendResponse($data->toArray(), 'Model retrieved successfully.');
}
private function retrieveModelAndRelations($name, $id)
{
$modelName = 'App\Models\\'.$name;
$model = new $modelName;
if ($id === 'null') {
...
} else {
$data = $modelName::when(isset($model->getRelations()['customer']), function($query) {
return $query->with('customer');
})...
})->when(isset($model->getRelations()['rateTables']), function($query) {
return $query->with(array('rateTables' => function($q) {
$q->orderBy('cashStart', 'ASC');
}));
})->when(isset($model->getRelations()['rateRows']), function($query) {
return $query->with(array('rateRows' => function($q) {
$q->orderBy('rate', 'ASC');
}));
})->when(isset($model->getRelations()['rateRistournes']), function($query) {
return $query->with(array('rateRistournes' => function($q) {
$q->orderBy('ristourne', 'ASC');
}));
})->find($id);
}
return $data;
}
And there is the result into the console:
created_at:(...)
deleted_at:(...)
id:(...)
institution_id:(...)
name:(...)
rate_ristournes:Array(1)
rate_rows:Array(1)
rate_tables:Array(1)
The 3 last line should be:
rateRistournes:Array(1)
rateRows:Array(1)
rateTables:Array(1)
Is there a way to force laravel to keep the relation key as I wrote it?
Something under the hood change the name and I don't know how to bypass it.
Change $snakeAttributes:
class Rate extends Model
{
public static $snakeAttributes = false;
}
I want to remove empty array when its return. I have been trying in many different ways, help plz
My controller looks :
public function index()
{
return JobsResource::collection(Jobs::all())->filter();
}
my resource file look:
class JobsCollection extends Resource
{
public function toArray($request)
{
$applicants_count =Job_applicants::where('job_id',$this->id)->get()->count();
if ($applicants_count>0) {
return [
'id' => $this->id,
'title' => $this->title,
'deadline' => $this->deadline,
'applicants_count' => $applicants_count,
'applicants' => new EmployeesResource($this->Employeess->take(2))
];
}
}
}
it always return an empty array
output :
[
[],
{
"id":99,
"title":"Construction Administrator - The Woodlands",
"deadline":"2018-06-30",
"applicants_count":10,
"applicants":[
{
"name":"Mr. Job Seeker",
"pivot":{
"job_id":99,
"employee_id":1
}
},
{
"name":"Michale Feil",
"pivot":{
"job_id":99,
"employee_id":2
}
}
]
}
Controller:
public function index() {
$jobs = Jobs::has('Employeess')->with('Employeess')->withCount('Employeess')->get();
return JobsResource::collection($jobs);
}
Resource file:
class JobsCollection extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'deadline' => $this->deadline,
'applicants_count' => $this->Employeess_count,
'applicants' => new EmployeesResource($this->Employeess->take(2))
];
}
}
While I am testing yii2 multiple select2 tutorial, I faced the error "Class 'common\models\LinkAllBehavior' not found".
Here is my model code,
public $tag_ids;
public static function tableName()
{
return 'post';
}
public function rules()
{
return [
[['title', 'body','tag_ids'], 'required'],
[['body'], 'string'],
[['title'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{return [
'id' => 'ID',
'title' => 'Title',
'body' => 'Body',
];}
public function behaviors()
{
return [
LinkAllBehavior::className(),
];
}
public function afterSave($insert, $changedAttributes)
{
$tags = [];
foreach ($this->tag_ids as $tag_name) {
$tag = Tag::getTagByName($tag_name);
if ($tag) {
$tags[] = $tag;
}
}
$this->linkAll('tags', $tags);
parent::afterSave($insert, $changedAttributes);
}
public function getPostToTags()
{
return $this->hasMany(PostToTag::className(), ['post_id' => 'id']);
}
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('post_to_tag', ['post_id' => 'id']);
}
}
So, I would like to know what is LinkAllBehavior and how does it work?
Thanks
LinkAllBehavior::className()
above line is causing this, if you are using yii2-linkall
try adding
use cornernote\linkall\LinkAllBehavior;
on top of your model code along with other use statements
I am using YII2 advanced application template with yii2-user.
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
This will set the current timestamp value in my user model. But I want to add this only if it's null; it should not be overwritten if I set the value in my controller.
You can create your TimestampBehavior with custom logic:
<?php
namespace app\behaviors;
use yii\db\ActiveRecord;
use yii\base\Behavior;
use yii\db\Expression;
class ARTimestampBehavior extends Behavior
{
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
];
}
public function beforeInsert($event)
{
$model = $event->sender;
if ($model->hasAttribute('created_at') && is_null($model->created_at)) {
$model->created_at = new Expression('NOW()');
}
if ($model->hasAttribute('updated_at')) {
$model->updated_at = new Expression('NOW()');
}
}
public function beforeUpdate($event)
{
$model = $event->sender;
if ($model->hasAttribute('updated_at')) {
$model->updated_at = new Expression('NOW()');
}
}
}
And then use it in your model:
public function behaviors()
{
return [
ARTimestampBehavior::className(),
];
}
I don't think there is an easy way to do this. The closest one can get without much coding it to specify a custom value to be set.
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'value' => function($event) {
return (/* some condition */)
? your_custom_function_returning_the_time()
: time();
],
];
}
That being said, I see this as a potential misuse of TimestampBehavior. One might be better off defining a new column for your custom creation timestamp.
If you are set on using the current column, then ditch TimestampBehavior and overwrite the beforeSave method of your model:
public function beforeSave($insert)
{
if (! parent::beforeSave($insert)) {
return false;
}
if ($insert && this->create_at === null) {
$this->create_at = time();
}
$this->update_at = time();
return true;
}
Just use default TimestampBehavior like this:
/**
* #inheritdoc
* #return array mixed
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => false,
'value' => function($event) {return $event->sender->created_at ?? new \yii\db\Expression('NOW()');},
],
];
}
Yii does have built-in support for preserving already filled values from v2.0.13:
public function behaviors()
{
return [
'timestamp' => [
'class' => \yii\behaviors\TimestampBehavior::class,
'preserveNonEmptyValues' => true,
],
];
}