I have a table called offers and this is how I store in my controller:
$offer = Offer::Create([
'user_id' => Auth::id(),
'property_id' => $request->property_id,
'offer_price' => $request->offer_price,
'offer_period' => $request->offer_period,
'offer_message' => $request->offer_message,
'cash_amount' => $request->cash_amount,
'buyer_type' => $request->buyer_type,
'expires_at' => now()->addMinutes($request->offer_period),
]);
I want to delete all the offers where the expires_at is older than the updated_at date. I am using the timestamps.
This is what I have in my kernel:
$schedule->call(function () {
Offer::where('updated_at', '>', 'expires_at' )->delete();
})->everyMinute();
When I ran the command:
php artisan schedule:run
I noticed that all the offers were deleted from my database.
I ran the command at 11:22, and even the rows that were meant to expire at 11:30 were deleted.
Is there anything that I possibly did wrong?
Instead of:
Offer::where('updated_at', '>', 'expires_at' )->delete();
you should use:
Offer::whereColumn('updated_at', '>', 'expires_at' )->delete();
Initially you used simple where. Laravel doesn't know that expires_at is here column name so where would be something like this:
SELECT * FROM offers where updated_at > 'expires_at'
And using second method Laravel will generate:
SELECT * FROM offers where updated_at > expires_at
without quotes (ant this is what you need here).
Instead of second method you can also use DB::raw construction like this:
Offer::where('updated_at', '>', \DB::raw('expires_at'))->delete();
Related
I am using neo4j version 3.X, I am using searchkick
currently using
User.search(params[:term], operator: 'or',
fields: [:first_name, :last_name],
misspellings: { below: 5 },
match: :word_start,
page: params[:page], per_page: params[:rpp], padding: params[:offset])
instead of
User.where('(sp.first_name + sp.last_name) =~ ?', /.*#{params[:term].sub(/\s/, '')}.*/i)
But I have problem where I have to make more cypher queries at the same time with searching How to do that?
For exmaple
Neo4j::ActiveBase.new_query.match(n: {User: { uuid: current_user.uuid }}).break
.match('(n)-[:connected_to {status: 2}]-(sp:User)')
.return('DISTINCT sp')
I want to seach in this query with elasctic search with first name & last name
In my model I have defined searchkick word_start: [:first_name, :last_name]
It's been a while since I've used searchkick, but I would suggest trying to use the ActiveNode syntax instead of the neo4j-core Query syntax so that you can append .search on the end:
current_user.connected_users.rel_where(status: 2).distinct.search(...)
This is assuming that there is a connected_users association which uses the connected_to relationship type. You may have another, similar, association in your User model but I didn't know what it was.
I'm not 100% sure if the distinct will work with the search or not, but you could perhaps drop the .distinct part and searchkick might return you a distinct set anyway.
I'm building Twitter Search API query based on their instructions here and here.
The query is working fine if I use the exact examples of the instructions above, like if I send the query like this: q=%23superbowl&result_type=recent, it would give me as many answers I'd like to get
BUT
if I would try to search for hashtag jäniksenvuosi (which I know to be tweeted many times), I would only get one answer and that answer is my own latest tweet. Nothing else.
I tried it with other Finnish words with umlauts too, but those work properly, so it is not about umlauts either.
Question in short
What's the difference between these two and how do I fix this?
Working: q=%23superbowl&result_type=recent
Not working: q=%23j%C3%A4niksenvuosi&result_type=recent
Search metadata
superbowl:
[search_metadata] => Array
(
[completed_in] => 0.075
[max_id] => 688984569614327808
[max_id_str] => 688984569614327808
[next_results] => ?max_id=688976241051873280&q=%23superbowl&count=30&include_entities=1&result_type=recent
[query] => %23superbowl
[refresh_url] => ?since_id=688984569614327808&q=%23superbowl&result_type=recent&include_entities=1
[count] => 30
[since_id] => 0
[since_id_str] => 0
)
jäniksenvuosi
[search_metadata] => Array
(
[completed_in] => 0.052
[max_id] => 688984569614327808
[max_id_str] => 688984569614327808
[query] => %23j%C3%A4niksenvuosi
[refresh_url] => ?since_id=688984569614327808&q=%23j%C3%A4niksenvuosi&result_type=recent&include_entities=1
[count] => 30
[since_id] => 0
[since_id_str] => 0
)
Just tried this using twurl, and I currently get three results for that hashtag (#jäniksenvuosi).
This is consistent with what I'd expect. On the Twitter website, there are three Tweets returned that contained that hashtag during January. The previous occurrence was in December.
Twitter's Search API has access to an index of Tweets covering approximately 7 days, and it is optimised for recency rather than completeness (see this page in the developer documentation)
How to get Last executed query after model save, update, delete in yii.
i.e:
$model->save();
like $this->db->last_query(); in CI
Thanks
Put this on your config.php file , you can see other details along with your query ...
'db'=>array(
'enableProfiling'=>true,
'enableParamLogging' => true,
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
…
array(
'class'=>'CProfileLogRoute',
'levels'=>'profile',
'enabled'=>true,
),
),
),
The simplest way to show last executed query is to make an sql error in that query! :p Give an invalid column name in query, then Yii error reporting will show that query as error, but you can see that query.
I have two find statements and need the results of one find statement to use in the second find statement however the two methods I have tried to use have come back with errors
here is the first find statement, it lists the sender_id's
$sender=$this->Invoice->Find('list', array('fields'=>('sender_id')));
here is the second find statement, it takes that list of sender_id's and returns the corresponding company_name
$senderName=$this->Account->Find('all', array(
'conditions' => array(
$sender=>'account.id')));
this returns the right information however returns this error Warning (2): Illegal offset type [APP\Controller\InvoicesController.php, line 185]
so i tried doing it this way
$senderName=$this->Account->Find('all', array(
'conditions' => array(
'id'=>$sender['Invoice']['sender_id'])));
and get an undefined index on invoice.
$senderName=$this->Account->Find('all', array(
'conditions' => array(
'Account.id' => array_values($sender),
),
));
The key is the field and the value is, well, the value(s).
In CodeIgniter I would do:
print_r ($this->db->queries);
In Yii I tried:
print_r (Yii::app()->db)
But this doesn't show any queries.
UPDATE:
I understand my problem: when I want to show db queries on a POST action, I don't show it. When using GET, it's ok.
As #bool.dev said, you can use CWebLogRoute or in my case i use CFileLogRoute to store these queries in file.
array (
'class' => 'CFileLogRoute',
'categories' => 'system.db.*',
'logFile' => 'sql.log',
),
To complement #snippLeaf-com's answer, you can trace this file filtering by the keywords you want like this:
// filter by "INSERT" or "UPDATE"
$ tail -f /path_to/protected/runtime/sql.log |grep 'INSERT\|UPDATE'
// filter (case insensitive) by "SELECT" in table "x2_users"
$ tail -f /path_to/protected/runtime/sql.log |grep -i SELECT.*x2_users
OBS: to get fresh data you could need refresh database cache:
rm -f protected/runtime/cache/*.bin
If you really want every query log in yii use yii db profiler extension.
Step1. Download extension from --> link
Step2. Unpack to protected/extensions/
Step3. Rename folder name yii-db-profiler-master to db_profiler
Step4. Update the following to your protected/config/main.php:
<?php
return array(
// …
'components' => array(
// …
'db' => array(
// …
'enableProfiling'=>true,
'enableParamLogging' => true,
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
// …
array(
'class'=>'ext.db_profiler.DbProfileLogRoute',
'countLimit' => 1, // How many times the same query should be executed to be considered inefficient
'slowQueryMin' => 0.01, // Minimum time for the query to be slow
),
),
),
),
);