Codeigniter active record where() with serialize data and regex - regex

i want to fetch all the records against a specific user_id, in my table user data is in serialize form in a column (user_data).
a:9:{s:9:"auth_type";s:4:"user";s:7:"user_id";s:2:"40";s:9:"user_type";s:1:"2";s:5:"fname";s:9:"Saif Alvi";s:5:"sname";s:0:"";s:8:"nickname";s:8:"saifalvi";s:6:"gender";s:0:"";s:13:"user_type_str";s:8:"employer";s:7:"lang_id";s:1:"1";}
to fetch all records against user_id i am using below query
$result = $this->DB->select("COUNT(*) AS sessionCount")->from(SESSIONS_TABLE)->where('user_data', REGEXP.'s:7:"user_id";s:"^[0-9]+":'.'"$user_id"')->get()->result_array();
where i am wrong or any other solution to use codigniter active record where() with serialize data and regex

Related

Doctrine 2 how to count the Users that partecipate to an Event? (so a COUNT on GROUP BY)

I have 3 tables in my DB:
user (_id, name)
event (_id, name, ...)
events_partecipants(user_id, event_id)
I have two Doctrine entities which maps those tables and their relations and everything works (eg. I'm able to get all the partecipants for a specific event).
Now, I want to retrieve the number of events each user joined to. Using pure SQL the query will be:
SELECT user_id, COUNT(*) as count
FROM events_partecipants
GROUP BY user_id
ORDER BY count DESC
As result, I want to retrieve also the name of each user, so I can send back into my JSON information about each user with the name, and not the ID.
If I want to use Doctrine how this can be achieved? I cannot find a smart way to do that.

getting most recent by joining subquery in Django ORM

I have a table of log entries that have user_id and datetime. I'd like to make a query to fetch the most recent of each log entry by user_id. I can't seem to figure out how to do that...
The SQL for the query would be something like this:
SELECT *
FROM table a
JOIN (SELECT user_id, max(datetime) maxDate
FROM table
GROUP BY user_id) b
ON a.user_id = b.user_id AND a.datetime = b.maxDate
Right now I'm doing it with a raw query, but I'd like to use the ORM's methods.
I suppose this should do
Table.objects.order_by('-user_id').distinct('user_id')
See distinct() in this -> https://docs.djangoproject.com/en/1.9/ref/models/querysets/
But this will only work if the latest log entry for that user is the last entry by that user in the table, i.e., the log entries of a particular user as sorted in ascending way in the table.
You may try:
User.objects.latest().id
Table.objects.order_by('user_id', '-datetime').distinct('user_id')
Add indexes to user_id and datetime (Meta.index_together = ['user_id', 'datetime']).

How to interact with Existing database with Model through template function in Django

I have an existing table called empname in my postgres database
(Projectid,empid,name,Location) as
(1,101,Raj,India),
(2,201,David,USA)
So in the app console it will have like the following
1)Projectid=Textbox
2)Ops =(view,insert,Edit)-Dropdown
Case1:
So if i write project id as 1 and select View Result:It will display all the records for Projectid =1(Here 1 record)
Case2:
If i write projectid as 3 and select insert it will ask for all the inputs like empid,name,address and based on that it will update the table .
Case3:
If i write projectid as 2 and select edit.Then it will show all the field for that id and user can edit any column and can save which will update the records in backend for the existing table
If there is not data found for the respective project id then it will display no records found
Please help me on this as I am stuck up with models
Once you have your models created, the next task should be the form models. I can identify atleast 3 form classes that you will need to create. One to display the information(case 1), another to collect information(case 2) and the last class to edit the information. Wire up the form to the views and add the urls.
A good reference could be a django a user registration form since it will have all the three cases taken care of.http://www.tangowithdjango.com/book17/chapters/login.html

DynamoDB query table for two indexed fields

In my dynamoDB table I have two fields which I intend to query for a specific value. One will returns several results, the second will return one result (aka one row).
I already have a range key field which is the date that the raw inserted.
What are my options if I want to have two fields to query by a single field?
From my understanding, there is an option to create a secondary index, but I must query it alongside with the hash key, which is not what I need. Is there any option to achieve that in a single table instead of creating a second table for that query?
Example Folders table:
user_id (hash key)
date_created (range key)
folder_id
First query: Select all folders Id's for a specific user order by date descending
Second query: Select single raw for a specific folder_id
If I use secondary index on the folder_id, I will have to query it alongside the user_id (hash key), which is as you can see, not what I want to achieve.
Thanks.
You need a Global Secondary Index.
folder_id (hash key)
date_created (range) - optional
Then you can also query by the folder_id
As you said yourself, you can't use LSI in this scenario.
You will have to create another table where folder_id is the hash key and the rest of the data as attributes.
You can also query for one primary key that will return the least amount of results than filter out the rest in code.

query the city field in Venue

I wrote this query for retrieving all the public events in Milan:
search?fields=name,venue&type=event&q=milan
But I need to query the city field in Venue. Is it possible?
$fql_query = 'https://graph.facebook.com/search?q=milan&type=event&'.$access_token;
This fql query will search all events that have a word "milan"