Syntax error with execute a query with one to one - doctrine-orm

I have two entities:
User entity with two fields are: id (#id), username
Profile entity with two fields are: user(#OneToOne,targetEntity="User"), fullname
But when I make a query try from things I read an JPA book:
SELECT p from Profile p where p.user.username = 'john'
It alerts to me a message:
[Syntax Error] line 0, col 55: Error: Expected =, <, <=, <>, >, >=, !=, got '.'

As of the current EBNF, the syntax you used is invalid in Doctrine 2 ORM. You will have to join the related entity as following:
SELECT
p
FROM
Profile p
JOIN
p.user u
WHERE
u.username = :username

Related

Django find all rows matching 2 columns criteria

Imagine the model Event like this
name
email
A
u1#example.org
B
u1#example.org
B
u1#example.org
C
u2#example.org
B
u3#example.org
B
u3#example.org
A
u4#example.org
B
u4#example.org
I would like to find all emails that contain name A and B. In my example ["u1#example.org", "u4#example.org"]
Today I'm doing
emails = [
e["email"]
for e in models.Event.objects.filter(name__in=["A", "B"])
.values("email")
.annotate(count=Count("id"))
.order_by()
.filter(count__gt=1)
]
It's not working because I'm also getting duplicates of emails containing only one name (like u3#example.org).
After trying different approach, I found the solution
events = ["A", "B"]
emails = [
e["email"]
for e in models.Event.objects.filter(name__in=events)
.values("email")
.annotate(count_name=Count("name", distinct=True))
.order_by()
.filter(count_name=len(events))
]
I need to group by email and count number of distinct name and filter by count equals to my number of events.
If you don't need the model, there is this option, that yields the expected result:
from django.db import connection
def get_random_events(request):
cursor = connection.cursor()
cursor.execute("SELECT DISTINCT email FROM event WHERE name = 'A' OR 'B'")
for row in cursor:
print(row[0])
return render(request, 'blank.html')
As for the ORM the problem is the last part of the query, it does not seem possible to properly build the WHERE clause. My best attempt was using Q lookups, still...the same problem:
RandomEvent.objects.values('email').distinct().filter(Q(name='B') | Q(name='A'))
# Query Structure
SELECT DISTINCT email FROM random_event WHERE (name = 'B' OR name = 'A')

How to I write a join to constrain on an attribute of the target of a belongs_to?

The model Reading.rb includes belongs_to :reader
I'm trying to implement a filter on 'index' of the readings by joining through the association:
readings = Reading.all.order(:date)
if params[:reader_name]
readings = readings.join(:reader).where('reader.name like ?', params[:reader_name])
end
I get:
no implicit conversion of Symbol into String
readings.joins(:reader).where('readers.name like ?', params[:reader_name])
As pointed out by bmargulies joins has a plural S at the end, and the table name is also plural.

How to reference elements from a joined table in a DQL statement

I have two entities (AdminMembers\Entity\Members and AdminEvents\Entity\Invitees) that are joined with a OneToMany relationship. Because the relationship is defined in the entities, I can use the following DQL statement to query the data:
$dql = "SELECT i FROM AdminEvents\Entity\Invitees i WHERE i.eventID=$eventID";
And, through this configuration I can use statements like $invitee->getMember()->getMemberLastName() in my ZF2 view script to get the data from the joined entities.
Now, if I want to sort the data in the DQL statement by fields in AdminMembers\Entity\Members, I run into a problem. The following statement
$dql = "SELECT i FROM AdminEvents\Entity\Invitees i WHERE i.eventID=$eventID ORDER BY i.memberLastName, i.memberFirstName";
throws the error message
Class AdminEvents\Entity\Invitees has no field or association named memberLastName
This is because the fields memberLastName and memberFirstName are defined in AdminMembers\Entity\Members. Although an association does exist, I'm certain I’m just missing some syntax in referencing memberLastName and memberFirstName.
I know that I can expand the DQL statement to join the entities in the DQL statement, which would allow me to identify the joined table and relate the elements to table identifier. But, since the tables are already joined in the entity definitions, they shouldn’t have to be joined again.
Is there a special syntax for referencing joined-table entities in the DQL statement without "rejoining" the tables in the statement?
You should join the members entity to be able to sort by its fields. See docs:
$dql = "SELECT i, m FROM AdminEvents\Entity\Invitees i JOIN i.member m WHERE i.eventID=$eventID ORDER BY m. memberLastName, m.memberFirstName";

doctrine manyToMany Query on Inverse Side

I've got 2 entity with Many To Many Relationship. They are Mapped correctly, both sides.
When I'm querying the inverse side with an Entity of the Owning side I've got the following error:
ContextErrorException: Notice: Undefined index: joinColumns in /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1528
I'm using the "built-in" doctrine findBy Methods, in this case: "findByInverseSide($InverseSideEntity)"
I solve this By creating a DQL query with Left Join on the InverseSide of The Table:
EX:
...
$qb = $this->createQueryBuilder('q');
$qb->leftJoin('q.inverseSide', 'i')
->where('i.id = :inverseSide_id')
->setParameter('inverseSide_id', $inverseSide_id);
...

Django query - join on the same table

i have a mini blog app, and a 'timeline' . there i want to be displayed all the posts from all the friends of a user, plus the posts of that user himself.
For that, i have to make some kind of a 'join' between the results of two queries (queries on the same table) , so that the final result will be the combination of the user - possessor of the account, and all his friends.
My query looks like this:
blog = New.objects.filter(created_by = following,created_by = request.user)
By that ',' I wanted to make a 'join' - I found something like this on a doc- but this method is not correct - I'm getting an error.
How else could be done this 'join' ?
You can use Django Q (query parameter) objects to create complex queries with logical operators (&, |, and ~). You can also use __ (double underscore) to join in query parameters.
# is this what you want?
blog = New.objects.filter( Q(created_by__followed_by = request.user)
|Q(created_by = request.user) )
For more details see http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects.