Ember Sort my Records so the null for a column are retuned first - sorting

I want to filter and then sort my ember records such that the null records for a column are returned first. I'm not much familiar with ember and have a rails back ground.
filteredByData = myLeads.filterBy('status', 'open').filterBy('doNotCall', false).filterBy('phoneValid', true)
filtered = filteredByData.sortBy 'last_dialed_at', 'last_name', 'first_name', 'id'
Right now, my records are getting ordered according to the id.
What i want is to implement the NULLS LAST/FIRST from sql or postgres in any query (such a s SELECT * FROM t1 ORDER BY c1 DESC NULLS FIRST here in my sort so i get the records such that the last_dialed_atnull records come first.
I know the same can be implemented in rails like
Foo.order('last_dialed_at DESC NULLS FIRST') but i have no idea for same in Ember
Any help will be appreciated. Thanks in Advance.
P.S: I'll try to create a ember twiddle meanwhile to explain myself better.

Although you can resort to a SortableMixin/ArrayProxy, those are lower-level APIs than you should really need to use. sortBy works fine for a single value, but I generally use computed.sort when I need to sort multiple things. Take a look at the code and link in this answer https://stackoverflow.com/a/46788377/334913 they should get you going.
One of the common ways Ember suggests doing things is to do computed properties for each step (or set of steps) as it makes it far easier to reason about.
So I would do something along the lines of:
filteredLeads: Ember.computed('leads', function() {
return filteredResultsHere;
}),
sortDefinition: [], // put your entries from your SortableMixin here
sortedFilteredLeads: Ember.computed.sort('filteredLeads', 'sortDefinition'),

I solved my issue by using the Ember.SortableMixin instead of sortBy. I ended up with the following code and this worked perfectly for me.
leads = Ember.ArrayProxy.createWithMixins Ember.SortableMixin, content: filteredByData, sortProperties: ['lastDialedAt', 'firstName', 'lastName', 'id'], sortAscending: true
I'm not sure or much aware why this worked and the fundamentals to an extent that i can explain this behaviour. If there's any explanation for same from anyone, please feel free to put your answer below and i'll accept it as the answer.
Thanks

Related

I want to check duplication value during insert time without using unique keyword

i make one table for with some column with nullable.
i already tried with two different query. one using
Register_member::where('passport',$passport)->orWhere('adharcardnumber',$adharcardnumber)->get();
and second DB::table type query.
$row = Register_member::where('passport',$passport)->orWhere('adharcardnumber',$adharcardnumber)->get();
if (!empty($row))
{
return response()->json(["status"=>0, "message"=>"Adharcard or Paasport number already exit."]);
}
if (empty($row))
{
Register_member::insert(['first_name'=>request('first_name'), 'middle_name'=>request('middle_name'), 'last_name'=>request('last_name'), 'adharcardnumber'=>request('adharcardnumber'), 'ocipcinumber'=>request('ocipcinumber'), 'passport'=>request('passport'), 'birthday'=>request('birthday'),
'mobilecode'=>request('mobilecode'), 'mobilenumber'=>request('mobilenumber'), 'email'=>request('email'), 'address'=>request('address'), 'landmark'=>request('landmark'), 'area'=>request('area'),
'gender'=>request('gender'), 'pincode'=>request('pincode'), 'city_name'=>request('city_name'), 'state_id'=>request('state_id'), 'country_id'=>request('country_id'), 'sampraday'=>request('sampraday'), 'other'=>request('other'), 'sms'=>request('sms')]);
return response()->json(["status"=>1, "message"=>"Member register successful."]);
}
if adharcardnumber or passport number are exists in table, then nagetive response. if in both any one in unique then, insert data in table
Let me suggest you something which I think serve you as a good solution. You can use the unique with required and regex. In this way it will use the already recommended ways of Laravel which are the best.
As an example for your adhaar card,
the validation should look like this
$request->validate([
'adhaar ' =>['required','unique:users','regex:/\d{12}/'],
]);
where adhar is the filed name where adhaar number is entered. Be sure to use validator like this use Illuminate\Support\Facades\Validator;. Also $request is the instance of the Request.
Using the required prevent empty field submission and the regex will throw an error if the pattern is not matched completely. so I think it would be a better a way to handle the scenario.
the above solution will work in both adhaar and passport. But for the passport the regex will be different though.
Please note these are all demo examples, you might need to modify it according to your needs. I use https://www.phpliveregex.com/ for regex making and checking and it is good enough.
I hope you get an idea of how to begin but if you need more information then let me know in the comments.

grails sort by date not working

I have domain object named Roll and on the list page i want to show the user all the Roll objects iterating through a list, sorted by entry date.
Here is the code i am using
[rollList: Roll.findAll(sort:"rollDate"){userid==uid}]
rollDate is a field inside the Roll object with data type java.util.Date
Any suggestion on why the output is not sorted by rollDate. When i iterate through the rollList on the gsp page it is not sorted.
Also, on the Roll domain object i have even put this code, but it's still not sorting.
static mapping = {
sort "rollDate"
}
Thank you.
Why aren't you using the dynamic finders?
Roll.findAllByUserid( uid, [ sort:"rollDate", order: 'desc'] )
should work.
The findAll( Map, Closure ) method appeared not a long time ago, perhaps it was not tested well...
You might need to use order in your query as well, then add order to it
[rollList: Roll.findAll(sort:"rollDate", order: 'desc'){userid==uid}]
After trying both the solutions mentioned it still didn't work. So i thought something might be wrong on the front end.
While researching more i found that since i was using jquery data tables it used to re order the sorting. The solutions i found was here
jQuery DataTable rows order
So both the answers above are correct. The issue was actually with jquery data tables.

How to massive delete in php-activerecord where id NOT in list?

I'm running a CodeIgniter application with PHP-Activerecord installed and I need to do a massive delete. The PHP-AR library supports massive update and delete, but the function only takes the ids that you want to delete/update. I'm trying to do a massive delete where the ids are NOT in the list.
delete from table where id not in (1,2,3...,500)
Unfortunately, the PHP-Activerecord website is of no help, and their forums are so horribly built (no search... seriously?) that I'm truly stuck.
edit: Please note, CodeIgniter's built-in ORM is not the same as PHP-Activerecord. Just thought I'd clarify that.
A bit late but this may help someone else. In PHPActiveRecord, do it in this way:
$ids_to_delete = array(1,2,3);
YourModel::delete_all(array('conditions' => array('id NOT IN (?)', $ids_to_delete)));
Ref: http://www.phpactiverecord.org/docs/ActiveRecord/Model#methoddelete_all
SInce active record is actually build string query with function, you can use this method:
- make your list as string
- make this query : $this->db->where('id not in',$string)->delete('table')

Getting a count of results with join, in active record

Conider this simple query:
$this->db->join('bids','bids.bid_for = request_quote.quoteid','left');
Is there any way possible to alter it, so instead of joining the result, it would instead join the COUNT of all bids it find?
I could rewrite this in plain SQL, but it is a part of bigger query, which I dont want to rewrite in SQL
Tough one. I've looked at the CI's source code, and I don't see a "hack" for that.
Perhaps you could use this CI's method:
$this->db->count_all_results();
Ok after messing around for couple hours I got it to work, so ill share it just in case anyone need it again:
public function getAllJobsByUserId($userid){
$this->db->select('quoteid')->select('quoteby')->select('country_from')
->select('country_to')->select('city_from')->select('city_to')
->select('countries.country_name, countries_to.country_name AS country_name_to')
->select('COUNT(\'bids.bid_id\') as bid_count');
$this->db->from('request_quote');
$this->db->where('quoteby',$userid);
$this->db->join('countries','countries.country_id = request_quote.country_from');
$this->db->join('countries as countries_to','countries_to.country_id = request_quote.country_to');
$this->db->join('bids as bid_count','bid_count.bid_for = request_quote.quoteid','outter');
$query=$this->db->get();
return $query->result();
}
This seem to work. But still in future ill probably write more complex queries in straight up SQL :)

cakephp 2.1 view associated data across all add.ctp, edit.ctp, view.ctp

I have an "examinations" table, its consultation_id relates to "consulsations" table, which in turn its consulation_id relates to the "patients" table.
Now, when I am in the add.ctp, edit.ctp or view.ctp of the "Examinations" Views I need to pull the "patients" details in so that some patient info can appear as to who the form pertains to as patient.
I have tried joins. Not to say they dont work. I am new to cakephp and I really need help as to how it will appear within the controller and how the view.ctp will display it.
I thought of elements but they are just .ctp files right?
Please if anyone can help regarding this it would be so appreciated. I've been trying to do this now for a week and I know there is something simple I am dont doing or thinking rights about.
So you just want to pull in related data? Pretty simple.
In your ExaminationsController methods.
$patients = $this->Examination->Consultation->Patient->find('all',
array('conditions'=>array('consultation_id'=>$id,'examination_id'=>$e_id)));
Something similar to this, not quite sure on which id you need to pass, as it will depend on how your models are linked up. http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
However, if your models are linked up properly, you should get this data anyway. If not set your models recursion to be higher.
$this->Model->recursive = 2;

Resources