Doctrine query by phone number in unknown format - symfony

Users store their phone numbers in different formats eg.: +1234567890 +1 (234) 567 890 etc.
I try to get user record from DB by phone number. Looks like I have to use Doctrine beberlei/DoctrineExtensions to make REGEX query but I don't understand how exactly to build query. Code below doesn't works.
$query = $this->createQueryBuilder('user')
->where('REGEXP(user.phone, :regexp) = :phone')
->setParameter('phone', preg_replace("/[^0-9]/", "", $phone ))
->setParameter('regexp', "[0-9]");

I have done extended work with parsing phonenumbers. When you store a phonenumber, you store it as a text. If you want to find a phonenumber, you use the LIKE clause. Also don't forget to enclose the phonenumber in quotes and %% signs.
Example WHERE clause:
WHERE phonenumber LIKE '%1234567890%'
So clean up the number and then use the above method to search for it.

Related

Querying part of a string in firebase

Is it possible to query just part of the name and get the data
Like I have data 12345678
can I somehow just search for 1234?
data.whereEqualsto("data", 1234);
This is needed because the last numbers changes and the first ones doesn't.
Is it possible to query just part of the name and get the data Like I have data 12345678 can I somehow just search for 1234?
Sure it is. When it comes to Firestore, you can simply use .startAt() as seen in the following query:
db.collection("collName").orderBy("data").startAt(1234);
When it comes to the Realtime Database, you can use .startAt() too, but as seen below:
db.child("nodeName").orderByChild("data").startAt(1234);
But remember, both queries will return elements that are greater than 1234. Since 12345678 is greater, it will be present in the result set.

Firebase query: exclude data where a specific field is null

I have a query that fetches data between 2 dates, using startAt (1 week ago) and endAt (now) on the last_visit fields.
Then I loop through the results to discard users who don’t have a profile picture.
Problem is around 20% of the users have a profile picture, so just to get 100 users with profile pictures, I have to query at least 500 people (I use limitToLast(500)).
Could I make this query more efficient, by somehow specifying something like in SQL: WHERE profile_picture IS NOT NULL?
If possible, I could also use only limitToLast(100) if it was possible to only take the users that do have a profile picture set.
Database looks like:
users: {
{user_uid}: {
profile_picture: null,
last_visit: 123456789
}
{user_uid}: {
profile_picture: 'example.com/pic.png',
last_visit: 123456789
}
}
If you're trying to exclude items that don't have a property, you need to query for the broadest range of values possible.
A simple example would be:
ref.orderByChild('profile_picture').startAt('!').endAt('~')‌​
This will capture most keys that consist of ASCII characters, since ! is the first printable character (#33) and ~ is the last printable character (#126). Be careful with these, because they won't work when your keys consist of unicode characters.

How shall I design this database?

So I've got to design a table for clients with fields (Id, name, bla, bla, Phone numbers). The last field terrifies me as there is not only one number, but many. I see 3 ways to accomplish this task
The field is String. Anytime before an insert, the String array of phone numbers is encoded using a delimiter ';' and thereafter inserted as String.
The field is BLOB. The string array is directly stored (no idea if this is possible in sqlite).
Create another table for Phone numbers with field (ClientId, PhoneNumber).
What seems the best approach?
As it is bad practice to store multiple values in one field, the third option stated is the regular way to go.

Can't store a korean string in database using LINQ

I'm using this code to store korean string in my database:
Dim username As String = Request.QueryString.Get("Some Korean String")
Using dg As New DataContext()
Dim newfriend As New FriendsTable With {.AskingUser = User.Identity.Name, .BeingAskedUser = username, .Pending = True}
dg.FriendsTables.InsertOnSubmit(newfriend)
dg.SubmitChanges()
end using
Checking my database, the username stored is a string"????"...
anybody got an idea how this happened or any workarounds?
What is your database collation? Are you able to store Korean strings with any other data access technology? What is the type of the username column, and is it accurately mapped in LINQ to SQL?
I suspect that something in the database isn't set up correctly to allow full Unicode. I very much doubt that this has anything to do with LINQ itself.
The other thing to check is that you're actually getting the right data in the first place. There are often several places where things can go wrong - you need to validate each place separately to see where the data is being corrupted. I have a short article on this which you may find helpful.
It sounds like you are storing Korean text in a varchar/text column which is not using a Korean collation. Thea easiest fix is to change the column type to nvarchar/ntext.
The nchar column types store Unicode data, whereas the char and varchar types store single byte characters in the specified collation.

SQL - querying via a textbox which could take different values

Developing a website and just trying to get back into the swing of (clever) SQL queries etc, my mind had totally gone tonight!
There is a website http://www.ufindus.com/ which has a textbox allowing you to enter either a place name or a postcode/zipcode. I am trying to do something similiar but I am rubbish at SQL - so how do you construct an SQL statement that could potentially look at 2 columns (i.e. place and postcode) because you can't query both fields for the same value e.g
place = 'YORK' AND postcode = 'YORK'
or
place = 'YO21 5EA' AND postcode = 'YO21 5EA'
so do you have to put some logic in to be intelligent enough to detect whether it looks like a place name or a postcode - that just seems too complicated to me!! Any help would be much appreciated.
You could use an "OR" to get the job done. For example,
place = 'YORK' or postcode = 'YORK'
You might also do better using the LIKE statement, as in
WHERE place LIKE 'YORK%' or postcode LIKE 'YORK%'
(this assumes both place and postcode are character-based columns)
why not use OR instead of AND?
place = #textboxvalue OR post = #textboxvalue
What's wrong with attempting to match on the place and postcode? If I put in 'York' and (somewhere) that happens to be a valid postcode, I should get that result. As for preventing the same thing being entered twice, well, you can handle that on the validation prior to doing the database call.
Ah. Guess I was a bit slow on the up-take. Yes... what the others suggested is right, 'OR' is what you were looking for. I misinterpreted.
Ok, first I'm assuming that you have a table with a mapping of postcodes to placenames.
Let's call this table 'postcode' with columns 'postcode' and 'postplace'. Both of these are of a char-type.
Then.. whatever you do, make sure the input from the user is not part of dynamic sql. Make sure it is a parameter. Otherwise, you are inviting SQL injection attacks that can really ruin your day. This is important.
Our user input is in #textboxstring.
Given this, you can get the postcode and postplace like this:
select #textboxstring = RTRIM(#textboxstring) + '%';
select postcode, postplace
from postcode
where postcode like #textboxstring or postplace like #textboxstring;
Note that I'm modifying #textboxstring to get wildcard match with like without having to use dynamic sql.
If the postcode was integer, you would need to convert the input to int before executing the sql. So with a #textboxint as well, you could do this:
select #textboxstring = RTRIM(#textboxstring) + '%';
select postcode, postplace
from postcode
where postcode = #textboxint or postplace like #textboxstring;
Oh, and you need to handle that your search can have multiple results. You probably only want the first row.

Resources