Getting the rows created before the selected one - php

I was wondering about the best way to get the count of all the rows created before the selected one. Right now I have defined an accessor that looks like this:
// In the model
public function getPositionAttribute() {
return self::where([
// Some other condition
['created_at', '<', $this->created_at->toDateTimeString()]
])->count();
}
// In the code
$model->position
It works correctly, but I'm worried about 2 things:
Is it a bad practice to call self on the model? Looks somehow off to me.
When called in a foreach this obviously generates a query for each element which is far from optimal. Is there any way to refactor this so that it can be eager loaded in a single query?
Bonus: I have totally discarded the idea of keeping a column with some kind of index because that initially sounded impossible to maintain, eg. when a record is deleted all the others should somehow shift position. Should I reconsider it? Is there a better way?

Pretty sure that using self here is the "best practice" because that is how that keyword was designed to be used.
In regards to refactoring, i personally can't think of optimizing the query as is but instead you could create a function that preloads all the position then use it normally. Assuming your model has a unique key 'id' and you are passing in a collection of model then, you can try something like this:
public static function populateOrderPositions($modelCollection){
// Optimize this query to include your "other condition"
$idCollection = Model::orderBy('created_at') // this will make it in the order of creation
->pluck('id'); // this will only retrieve the id field
// This array will contain an array with the model object ids as key and a numeric position (starts at 0)
$positionArr = $idCollection->flip()->all();
// Then just load all the position into the object and return it.
return $modelCollection->map(function($modelObj) use ($positionArr){
return $modelObj->position = $positionArr[$modelObj->id] + 1; // +1 because array key starts at 0
};
}
You would also need to adjust your attribute code to use the loaded attribute instead of ignoring the loaded attribute like so:
public function getPositionAttribute() {
return $this->attributes['position'] ?? self::where([
// Some other condition
['created_at', '<', $this->created_at->toDateTimeString()]
])->count();
}
With these changes, you can then prepopulate the position then use it afterward without the need to query the database.
These code are untested as i don't know how your model and query will be structured and is more of an example. Also you would need to compare the performance vs your original code.

Related

Lumen/Laravel get attribute when using select and with

I've run into this issue a couple of times now and I can't seem to find a solution - I feel like I'm missing something obvious.
I am making a JSON API with Lumen, building up specific routes for specific use cases. To speed up load times, I only want to output the fields I will be using.
My model has a combination of fields, relations and attributes. I am struggling to specify the attribute when limiting output
For example:
A task has time_records. Each time_record has a value of time.
I want to output the task names, time record values plus an attribute of totalTime which I have set on the task like the below (this isn't the full code but hopefully it gets across the idea)
class Task extends Model {
protected $appends = [
'totalTime'
];
public function timeRecords() {
return $this->hasMany('TimeRecord');
}
public function getTotalTimeAttribute() {
$total = $this->timeRecords()->map(function($time_record){
return $time_record->value;
});
return array_sum($total);
}
}
If I then do the following:
Task::get();
And output that, I get all the tasks with the totalTime attribute. However, as mentioned I want to only output some attributes:
Task::select('id', 'name')
->with([
'timeRecord' => function($query){
$query->select('id');
}
])
No matter what I do, I can't seem to get the attribute output with that. The value is there in the array, but is null.
To resolve this, you have to select the fields that the attribute accesses.
Because I was only returning the ID of the time_record, the attribute was unable to calculate the total_time as the value field was not available.
(Was only while writing this question out did I discover the answer... 🙄)
You can get that using with() method without callback. Callback method provide you to filter results..
Task::select('id', 'name')
->with("timeRecords:id") // use comma for multiple columns
->get();

Yii2 - Calling model functions on entire activeRecords?

What I am trying to do
I want to query a specific set of records using active model like so
$jobModel = Jobs::find()->select('JOB_CODE')->distinct()->where(['DEPT_ID'=>$dept_id])->all();
Then I want to assign a flag attribute to the records in this activerecord based on whether they appear in a relationship table
What I have tried
So in my job model, I have declared a new attribute inAccount. Then I added this function in the job model that sets the inAccount flag to -1 or 0 based on whether a record is found in the relationship table with the specified account_id
public function assignInAccount($account_id){
if(JobCodeAccounts::find()->where(['JOB_CODE'=>$this->JOB_CODE])->andWhere(['ACCOUNT_ID'=>$account_id])->one() == null){
$this->inAccount=0;
}
else{
$this->inAccount = -1;
}
}
What I have been doing is assigning each value individually using foreach like so
foreach($jobModel as $job){
$job->assignInAccount($account_id);
}
However, this is obviously very slow because if I have a large number of records in $jobModel, and each one makes a db query in assignInAccount() this could obviously take some time if the db is slow.
What I am looking for
I am wondering if there is a more efficient way to do this, so that I can assign inAccount to all job records at once. I considered using afterFind() but I don't think this would work as I need to specify a specific parameter. I am wondering if there is a way I can pass in an entire model (or at least array of models/model-attributes and then do all the assignations running only a single query.
I should mention that I do need to end up with the original $jobModel activerecord as well
Thanks to scaisEdge's answer I was able to come up with an alternative solution, first finding the array of jobs that need to be flagged like so:
$inAccountJobs = array_column(Yii::$app->db->createCommand('Select * from job_code_accounts where ACCOUNT_ID = :account_id')
->bindValues([':account_id' => $account_id])->queryAll(), 'JOB_CODE');
and then checking each job record to see if it appears in this array like so
foreach($jobModel as $job){
if(in_array($job->JOB_CODE, $inAccountJobs))
$job->inAccount = -1;
else
$job->inAccount = 0;
}
Does seem to be noticeably faster as it requires only a single query.

How to share a variable between master layout and a view

I have the following static method in my Guest model to retrieve all guests at location 1:
public static function getGuests()
{
return self::where('location_id', '=', 1)->get();
}
I also got the following static method to retrieve and display the number of guests at location 1.
public static function getCount()
{
return self::getGuests()->count();
}
I later call the getGuests() in my GuestController and pass all the guests to the view guests/index.blade.php to display data in a table.
I also call getCount() from my BaseController (see below) to make the count variable available both in the menubar in the layout/layout.blade.php file and the header of guests/index.blade.php.
View::share('numGuests', Guest::getCount());
By doing this i naturally end up with an expensive duplicated query to the DB since GetCount() calls getGuests(). Is there a better way to display the number of counts, both in the master layout file and the embedded view, without having to do the duplicate query call?
View::share shouldn't be calling the `Guest::getCount()' twice. It should just call it once and then pass it in. If not though can't you just cache the value in a variable and then pass it in? Then its definitely not getting queried twice.
$guestCount = Guest::getCount();
View::share('numGuests', $guestCount);
I might be misunderstanding here - are you also calling Guest::getCount() later on from your GuestController? Sorry - can't comment on main questions yet to ask.
I found a quick and low-cost solution by simply just calling the count() directly on the query, instead on the function call.
public static function getAll($locationId)
{
return self::whereLocationId($locationId)->with('country')->get();
}
public static function countAll($locationId)
{
return self::whereLocationId($locationId)->count();
}
This left me me with two separate queries, where the count is onsiderably lower cost:
select * from `guests` where `location_id` = '2'
and
select count(*) as aggregate from `guests` where `location_id` = '2'
It can probably be made even quicker by selecting only the neccesary columns instead of *. In cases where the query is more complicated and require more WHERE clauses, it can easily be kept DRY by using query scopes. Thanks for massaging my brain.

SQL SELECT * but with a twist - CodeIgniter

OK lets say I want to select a number of columns from a database table, but I won't know what those columns are in the method. I could pass them in, but it could be more or less depending on the method calling the database method.
A quick fix would be SELECT *, but I understand that this is bad and can cause more data to be returned than is necessary, and I definitely don't need all the data from that table.
So I am using CodeIgniter and prepared statements to do this, and below is what I have currently (it works, just point that out).
function get_pages() {
$this->db->select('pages.id, pages.title, pages.on_nav, pages.date_added, admin.first_name, admin.last_name')
->from('pages, admin')
->where('pages.admin_id = admin.id')
->order_by('pages.id', 'ASC');
$query = $this->db->get();
return $query->result();
}
It's a simple function, but at the moment limited to getting only 'pages'. I want to convert this to work with getting from other tables too. What is the best way?
Many thanks in advance.
EDIT In CodeIgniter I have many Controllers. One for 'pages', one for 'products', one for 'news' and on and on. I don't want to create a single database query method in my model for each controller.
i think the desire to not have 4 methods is misguided. if you don't have the information in the method, you'll have to pass it in. so you could either pass in a string with the table you want and switch over that changing the query based on the table name, or pass in all of the necessary parts of the query. this would include table name, criteria column, criteria, and columns to select. and you'd need to pass that information in every time you called the function. neither of those two methods are really going to save you much code, and they're both less readable than a function for each purpose.
The entire idea with models to put your specific queries to the persistence layer in there. Using a generic catch-all method can be disastrous and hard to test. You should shape your model around the problem you're trying to solve.
This makes it much cleaner and easier to work with. At the same time you must also avoid the common trap of over-sizing models. Each model should follow the SRP. Try and separate concerns so that in your controller, you can easily see state changes.
Does that make sense or am I just rambling...?
In your model:
function get_pages($table_source) {
$this->db->select($table_source.".id"); // or $this->db->select('id');
// for instance, if one of your $table_source ="users" and there is no 'title' column you can write
if($table_source!='users') $this->db->select('title');
$this->db->select('on_nav');
$this->db->select('date_added');
$this->db->select('admin.first_name');
$this->db->select('admin.last_name');
$this->db->join('admin','admin.id = '.$table_source.'.admin_id')
$this->db->order_by('pages.id', 'ASC');
$query = $this->db->get($table_source);
return $query->result_array();
}
In your controller:
function all_tables_info() {
$tables = array("pages","users","customers");
$i=0;
foreach($tables as $table) {
$data[$i++]=$this->your_Model->get_pages($table);
}
//do somthing with $data
}

Yii fetching records by type

I'm wondering if Yii has an efficient method for grouping items by type.
Let's say I have the following model:
Tag
------
id
name
type_id
And let's say there are 5 different types of Tags. I want to be able to display in my index all tags in sections by type_id. Is there a Yii-way of accomplishing this?
Outside a framework I would write a function such that results fetched from the DB were stored like this:
$tags[$typeID][] = $tag;
Then in each section I could do something like:
foreach( $tags[$typeID] as $tag )
{
// Here are all tags for one $typeID
}
But I'm having difficulty figuring out how to do this in Yii without:
A) looping through the entire result set first and rewriting it or,
B) running 5 different queries.
When using ActiveRecord simply specify the "index" in the DBCriteria. So in a query do:
ActiveRecordClass::model()->findAll(array('index'=>'type_id'));
That will return an assoc array that your after. TBF it probably executes exactly the same code, but this is obviously easier to use that performing it everywhere.
Assuming that your active record class is called MyActiveRecordClass, the simplest approach should be sufficient:
$models = MyActiveRecordClass::model()->findAll();
$groupedModels = array();
foreach ($models as $model) {
$groupedModels[$model->typeID][] = $model;
}
If you give more specific information about how you intend to display the grouped results it might be that a better approach can be worked out.

Categories