Is there a way to get the smallest and highest id in your table and check that in an if / else statement?
I already tried $user->first() and $user->last() but those did not work well.
Use the min/max functions of Eloquent.
User::max('id');
User::min('id');
This will get you the highest and User id's
User::orderBy('id', 'desc')->first()->id;
User::orderBy('id', 'asc')->first()->id;
I'm not exactly sure what you mean by "check that in an if / else statement." If you could provide a little more explanation I'd be able to help more.
Related
I dont know whats wrong but my code dont output the paid amount column
$payment_tbl = TableRegistry::get("MembershipPayment");
$payments = $payment_tbl->find();
$payments->select(['payment_total'=> $payments->func()->sum('paid_amount')]);
$this->set("payments",$payments);
and then echo this as echo $payments->payment_total;
$payments will be a query result object, not a single result. With this query, where you're expecting just a single row, add ->first() after your sum call.
In general, if you're not getting what you expect, dump the contents of the variable in question, like with pr($payments) or debug($payments), that will very often very quickly give you a clear indication of what the problem is. In this case, you'll see it's not the Entity object that you're expecting.
Sorry if the question was asked before i try search but not found. I'm still new on mssql so
maybe the answer are obvious just i cant find.
My current query is:
SELECT gold_min, gold_max, gold_min_2, gold_max_2
FROM m_rdb
I want to divide the values on current columns by 20 and then update, just not sure how to do.
Any help are welcome.
Are you looking for this:
update m_rdb
set col=col/20.0
If your updating the values to same columns then
Update m_rdb SET gold_min = gold_min/20, gold_max=gold_max/20,
gold_min_2=gold_min_2/20, gold_max_2=gold_max_2/20
If your updating the values to Other columns then
Update m_rdb SET gold_min_other = gold_min/20, gold_max_other=gold_max/20,
gold_min_2_other=gold_min_2/20, gold_max_2_other=gold_max_2/20
I have to calculate the agreement value for my new record .Assuming that agreement value is ending_days - begining_days. I have put this formula in my object.
IF(Beginning_c <> Ending_c,Ending_c-Beginning_c,0).
I have two questions related to this
1: What is the agreement value ?, Am i correct in assuming what i have assumed or does it some specific other meaning?
2: I have to create an agreement value for new records only. So, should i take care of this fact in my IF condition For example : should my IF code look something like this
IF (AND(//somehow check if the record is new,Beginning_c <> Ending_c),Ending_c-Beginning_c,0)
First, if Beginning__c == Ending__c then Ending__c-Beginning__c is already 0 so your formula does not need IF at all.
Second, formula is evaluated every time its referenced in SOQL, if you want a once-off calculation you should use before insert trigger or a workflow and store result in regular field.
To answer part 2 of your question. Use the function ISNEW() in you formula.
So your complete formula would be:
IF (AND(ISNEW(),Beginning_c <> Ending_c),Ending_c-Beginning_c,0)
I just inherited some cakePHP code and I am not very familiar with it (or any other php/serverside language). I need to set the id of the item I am adding to the database to be the value of the last item plus one, originally I did a call like this:
$id = $this->Project->find('count') + 1;
but this seems to add about 8 seconds to my page loading (which seems weird because the database only has about 400 items) but that is another problem. For now I need a faster way to find the id of the last item in the database, is there a way using find to quickly retrieve the last item in a given table?
That's a very bad approach on setting the id.
You do know that, for example, MySQL supports auto-increment for INT-fields and therefore will set the id automatically for you?
The suggested functions getLastInsertId and getInsertId will only work after an insert and not always.
I also can't understand that your call adds 8 seconds to your siteload. If I do such a call on my table (which also has around 400 records) the call itself only needs a few milliseconds. There is no delay the user would notice.
I think there might be a problem with your database-setup as this seems very unlikely.
Also please have a look if your database supports auto-increment (I can't imagine that's not possible) as this would be the easiest way of adding your wanted functionality.
I would try
$id = $this->Project->getLastInsertID();
$id++;
The method can be found in cake/libs/model/model.php in line 2768
As well as on this SO page
Cheers!
If you are looking for the cakePHP3 solution to this you simply use last().
ie:
use Cake\ORM\TableRegistry;
....
$myrecordstable=Tableregistry::get('Myrecords');
$myrecords=$myrecordstable->find()->last();
$lastId = $myrecords->id;
....
So, I have an autocomplete dropdown with a list of townships. Initially I just had the 20 or so that we had in the database... but recently, we have noticed that some of our data lies in other counties... even other states. So, the answer to that was buy one of those databases with all towns in the US (yes, I know, geocoding is the answer but due to time constraints we are doing this until we have time for that feature).
So, when we had 20-25 towns the autocomplete worked stellarly... now that there are 80,000 it's not as easy.
As I type I am thinking that the best way to do this is default to this state, then there will be much less. I will add a state selector to the page that defaults to NJ then you can pick another state if need be, this will narrow down the list to < 1000. Though, I may have the same issue? Does anyone know of a work around for an autocomplete with a lot of data?
should I post teh codez of my webservice?
Are you trying to autocomplete after only 1 character is typed? Maybe wait until 2 or more...?
Also, can you just return the top 10 rows, or something?
Sounds like your application is suffocating on the amount of data being returned, and then attempted to be rendered by the browser.
I assume that your database has the proper indexes, and you don't have a performance problem there.
I would limit the results of your service to no more than say 100 results. Users will not look at any more than that any how.
I would also only being retrieving the data from the service once 2 or 3 characters are entered which will further reduce the scope of the query.
Good Luck!
Stupid question maybe, but... have you checked to make sure you have an index on the town name column? I wouldn't think 80K names should be stressing your database...
I think you're on the right track. Use a series of cascading inputs, State -> County -> Township where each succeeding one grabs the potential population based on the value of the preceding one. Each input would validate against its potential population to avoid spurious inputs. I would suggest caching the intermediate results and querying against them for the autocomplete instead of going all the way back to the database each time.
If you have control of the underlying SQL, you may want to try several "UNION" queries instead of one query with several "OR like" lines in its where clause.
Check out this article on optimizing SQL.
I'd just limit the SQL query with a TOP clause. I also like using a "less than" instead of a like:
select top 10 name from cities where #partialname < name order by name;
that "Ce" will give you "Cedar Grove" and "Cedar Knolls" but also "Chatham" & "Cherry Hill" so you always get ten.
In LINQ:
var q = (from c in db.Cities
where partialname < c.Name
orderby c.Name
select c.Name).Take(10);