In yii how to find user's id by using email field - sql

In yii i am developing function.I am checking user entered email id whether it exists in database by query-
$record=User1::model()->find(array(
'select'=>'primaryEmail',
'condition'=>'PrimaryEmail=:email',
'params'=>array(':email'=>$_POST['email']))
);
if record exists then i want to retrieve that record's userid and SecurityQuestionId. So how to write search query.

$record=User1::model()->find(array(
'select'=>'id, securityQuestionId, primaryEmail',
'condition'=>'primaryEmail=:email',
'params'=>array(':email'=>$_POST['email']))
);
or just
$record = User1::model()->findByAttributes(array('primaryEmail' => $_POST['email']));

Related

Laravel Eloquent Getting Many To Many Relations Per Pivot Table Field Condition

I have a problem with Laravel 7 Eloquent. I have 2 tables joined by many to many relation. Table user is connected to PaymentModule by pivot table PaymentModuleUser. I need to use where on pivot table on statement is_active = 1. When i use toSQL() in my model SQL returns good results but when i check my Eloquent Query in Controller, data that i revicive ignore my wherePivot method (return all data from table ignoring is_active = 1 field subscribers = null and get me this user.. I must do it if my pivotWhere = null dont show this user). Could you point me where i get wrong with my code?
My UserTable model:
public function subscriber(){
return $this->belongsToMany(PaymentsModule::class, 'payment_module_user', 'user_id')->withPivot('is_active');
}
MyController:
$users = User::with(['subscriber'=>function($query)
{
$query->wherePivot('is_active','=', 1);
}])->get();
print_r($users);
Try with
$users = User::with('subscriber' => function($query) {
$query->where('payment_module_user.is_active', 1);
})->get();
print_r($users);
The pivot table is already joined by eloquent, so just start using it
Found it here.
In your controller, try using this
$query->wherePivot('is_active', 1);
Instead of,
$query->wherePivot('is_active','=', 1);

Can a SQL database support "insert only" mode?

In the latest years the "Insert Only" methodology came more and more popular.
For those who use SQL DB you probably know that in high volume with a lot of update queries the DB is locking the rows and you starting to get a "bottleneck". the Insert Only mode is to use only insert (without updates) and always retrieve the latest item in the DB.
The issue I'm facing is with the SELECT queries since there is a field that can be common for multiple records in the DB and if I will want to query by it I will never know when I got all of the latest records for the field above (unless I'm using GROUP and this will not be efficient)
Scheme Example:
let say I have the following scheme:
CREATE TABLE users
(
id SERIAL NOT NULL
CONSTRAINT users_pkey
PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
username VARCHAR(255),
email VARCHAR(255),
password VARCHAR(255),
account_id INTEGER,
created_at TIMESTAMP NOT NULL
);
Now let say I have the following users that's related to account number 1 (using account_id):
1. John Doe
2. Jain Doe
If I will want to edit John Doe last name in the Insert Only mode I will insert a new record and when I will want to retrieve it I will run the following query:
SELECT * from users WHERE email='jhon.doe#test.com' ORDER BY created_at Desc limit 1;
The issue is what I need to to if I want to retrieve all account 1 users ? how can I prevent from executing poor query with group by
The following query will return 3 records although I have only 2 users
SELECT * from users WHERE account_id=1;
The answer to your question is distinct on (in Postgres). However, it is unclear how you define a user. I would expect a user_id, but perhaps email is supposed to serve this purpose.
The query looks like:
select distinct on (email) u.*
from users u
where account_id = 1
order by email, created_at desc;
For performance, you want an index on users(account_id, email, created_at desc).

delete a line beginning with N characters SQL Postgres

I have 2 tables SQL:
USER with columns ->: uid, name, mail
Phone ->: id, brand, model, refFirtUser
refFirtUser is a column that constitutes the user's id - and a random number
example : refFirtUser (uid_users-12345687) -> 9145-12345687
i want to delete in table "Phone" all data .
I must to get "uid" of user by mail and to search N first characters before "-".
all data in column "refFirtUser " starting with "uid-xxxxx"
(where uid is the id of the user).
the only data I have is email
How should I do?
If you want to delete the from phone the user with mail 'someone#gmail.com':
delete from phone
where
refFirtUser like (select uid from user where mail = 'someone#gmail.com') || '-%'
It is ambiguous from your explanation whether user.uid is the number before - or after. In either case, you may use exists
delete from phone p
where exists ( select 1 from user u where mail in ( 'listofmail')
and p.refFirtUser like '%-'||u.id );
OR
delete from phone p
where exists ( select 1 from user u where mail in ( 'listofmail')
and p.refFirtUser like u.id||'-%' );

How do I write the following SQL query in Laravel using Eloquent?

I want to write a query in Laravel to retrive all the posts that a particular user has viewed. The user_id and the post_id is saved in the table named WatchHistory.
SELECT *
FROM Post
WHERE post_id = (
SELECT post_id
FROM WatchHistory
WHERE user_id = $user_id
);
I tried the following :
$posts = Post::whereIn('post_id', function($query){
$query->select('post_id')
->from(with(new WatchHistory)->getTable())
->where('user_id',$user_id);
})->get();
But it gives me an error that the variable user_id is not defined.
You should try this :
POST::whereIn('post_id', function($query) use($user_id){
$query->select('post_id')
->from(with(new WATHCHISTORY)->getTable())
->where('user_id',$user_id);
})->get();
Hope this work for you !!!!
Try this one,
If your user's id is in variable $user_id, you can do it like,
DB::table('post')->whereIn('post_id',
DB::table('watchhistory')->where('user_id',$user_id)->pluck('post_id')
)->get();

rails understanding sql code to write tests

I want to figure out what is checked with this method and then I will test according to this.
partner.rb
def get_record_count
self.administrator? ? ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id).size : self.contact_record.size
end
If that partner is an administrator then it matches it's company_id with fellow partners. Then it retrieves associated ContactRecords where partner_id = fellow ids and return back the size of total such records : number of matched contact records.
If it is not an administrator, it gives back it's contact_record.size
ActiveRecord substitute for:
ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ?)", self.company_id).size
ContactRecord.where(:partner_id => Partner.where(:partner_id => self.company_id).pluck(:id)).size
1.
SELECT id FROM partners WHERE company_id = ?
This part returns list of ids of "partners" with company_id = id of current Object (Model). Name this list as PARTNERS_LIST
2.
ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id)
And this part finds ContactRecords with "partner_id" belongs to PARTNERS_LIST.
3.
self.administrator? ? ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id).size
Finally this returns count of ContactRecords, finded at stage 2.