Is something wrong with my FQL? [closed] - facebook-graph-api

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I keep getting unexpected variable error. All i'm trying to do is fetch list of friends who are also app users
$user_profile = $facebook->api('/me','GET');
$fql = 'SELECT uid, name, is_app_user, pic_square
FROM user WHERE uid
IN
(SELECT uid2 FROM friend WHERE uid1 = me())
AND is_app_user = 1'
$param = array(
'method' => 'fql.query',
'query' => $fql,
);
$fqlresult = $facebook->api($param);
$fqlresult = $this->facebook->api($param);
foreach($fqlresult as $result)
{
print_r($result);
print($result['name']);
}

I keep getting unexpected variable error.
You mean a PHP error message – and you get a file name and line number with it.
And of course you are getting it – because you are missing a semicolon …
$fql = 'SELECT uid, name, is_app_user, pic_square
FROM user WHERE uid
IN
(SELECT uid2 FROM friend WHERE uid1 = me())
AND is_app_user = 1'
$param = array(
… after the declaration of $fql’s value, so that $param on the next line comes unexpected to the parser.
Dude, please get some basic PHP knowledge, before continuing to play around with complex APIs.

Your fql is correct, you can test it here FQL Query, I suggest you to check the php function calls as I see you have both
$fqlresult = $facebook->api($param);
$fqlresult = $this->facebook->api($param);
and I'm not sure what you want to achieve with the double call.
You can find more details on how to use FQL and the Facebook PHP SDK here https://developers.facebook.com/docs/reference/php/facebook-api/

Related

django left outer join filter non-matching records

The goal is to find out if a Survey Response doesn't have current answers for all top level questions.
This could be done by getting all top level questions for the Response, and then filter out the questions which don't have current answers.
I can write this in SQL but is there a way I can implement this using django's QuerySet interface?
Models
class Survey(Model):
...
class SurveySection(Model):
survey = ForeignKey(Survey, related_name='survey_sections')
class SurveyQuestion(Model):
survey_section = ForeignKey(SurveySection, related_name='survey_questions')
parent = ForeignKey('self') #questions can be nested
class SurveyResponse(Model):
survey = ForeignKey(Survey, related_name='survey_responses')
class SurveyAnswer(Model):
survey_response = ForeignKey(SurveyResponse, related_name='survey_answers')
survey_question = ForeignKey(SurveyQuestion, related_name='survey_answer')
is_current = BooleanField()
SQL
This should find all the top-level questions for the Survey the Response is for, getting the current answers that match those questions and removing the questions that don't have answers.
select * from survey_surveyquestion question
join survey_surveysection section on section.id = question.survey_section_id
join survey_survey survey on survey.id = section.survey_id
join survey_surveyresponse response on response.survey_id = survey.id
left outer join survey_surveyanswer answer on answer.survey_question_id = question.id and answer.is_current = true
where response.id = 16
and answer.id is null
and question.parent is null
You can take this approach:
First, get number of questions which has parent null:
top_question_count = SurveyQuestion.objects.filter(parent__is_null=True).count()
Then, use it in filter:
from django.db.models imprt Count
SurveyResponse.objects.filter(
survey_answers__survey_question__parent__is_null=True,
is_current=True
).annotate(
top_level_questions=Count('survey_answers__survey_question')
).filter(
top_level_questions=top_question_count
)

Doctrine JOIN syntax

I'm having an issue converting some legacy SQL for the doctrine query builder. I think the problem is in the inner join, but I can't quite work out the parameters the builder is expecting.
This is what I have so far:
$qb = $em->createQueryBuilder();
$qb->select('ob.size', 'ob.colour', 'ob.productId', 'p.title')
->from('m:Option', 'ob')
->innerJoin('m:Product', 'p', 'ON', 'ob.ProductId');
And this is the original query:
query="select size,colour,product_id,title from
products,options_new where
picture = '' and
products.id = options_new.product_id and
product_id like 'UTRW%'
group by product_id";
I normally write joins explicitly, so I'm not certain I'm understanding how the from clause is working here.
At the moment the new query is generating this error:
Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON'
Cheers!
Try the query below (drop here your entities, would be more helpful) and for more details about fetching related entities, check the docs: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#joins
$qb = $em->createQueryBuilder('m');
$qb->select('ob.size', 'ob.colour', 'ob.productId', 'p.title')
->innerJoin('m.product', 'ob');

django if a record has some self foreign key value, then operate on the foreign key record [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In order to avoid changing a lot of existing code, I try to add a self foreign key to a model, then the operations(filter,get,update,save,...) on the record will automatically performed on the "real record" if it has the self foreign key value.what need I do?
class Person(models.Model):
name = models.CharField(null=True,blank=True,max_length=50)
real_one = models.ForeignKey('self',null=True,blank=True)
I am expecting:
tom = Person.objects.create(name='Tom') # id=1
Person.objects.create(name='someone', real_one=tom) # id=2
someone = Person.objects.filter(name='someone')[0]
print someone.id # 1
print someone.name # Tom
someone.name = 'Jack'
someone.save()
tom = Person.objects.filter(name='Tom')[0] # sorry, this line added late
print tom.name # 'Jack'
IMHO, you're trying to do this in a denormalized fashion and it probably isn't going to work for you. One thing you could do is:
class Person(models.Model):
name = models.CharField(null=True,blank=True,max_length=50)
real_one = models.ForeignKey('self',null=True,blank=True)
#property
def real_name(self):
if(self.real_one):
return self.real_one.real_name
else:
return self.real_name
#real_name.setter
def real_name_setter(self, val):
if(self.real_one):
self.real_one.real_name = val
self.real_one.save()
else:
self.real_name = val
self.save()
but this creates a lot of database transactions. Note also the required side effect of the setter, since it has to save the record immediately lest it lose track of which record was changed. And this still has a problem in that the following case won't work:
someone.name = 'Jack'
someone.save()
print tom.name # 'Tom' <-- The 'tom' object now has stale data!
One object is being changed, yet (as far as Python is concerned) an entirely different object is really being changed. There's no reliable way to do this without changing Django and creating a mess (Django would have to internally track every object that referred to a given record, which plays havoc with the garbage collector, Django's queryset lazy-evaluation generators, and maybe even transactions).
Your best bet is to try a different approach, or live with the fact that your example isn't going to work. If you stick with this schema, every filter and exclude queryset call, and probably others, is going to need to know about this aliasing.
IMHO, you're much, much better off changing the schema: one table with names and one table with the "real" info for each person. The name table has foreign keys to the info table.
If you're trying to do something else, then you need to explain what a lot clearer in the question.

Showing the no. of entries in the template in Django [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
i created a form form simple entries of question , i wanted to show the total no. of questions entered in the template.
Can anyone show me how to do it ?
The best practice is to calculate this in your view and send the number to your template:
from django.shortcuts import render
from myapp.models import Entry
def form_view(request):
total_entries = Entry.objects.count()
# your other code
return render(request, 'template.html', {'total': total_entries})
In your template:
The total is {{ total }}
Use this syntax :-
<p>{{ question_list|length}}</p>

How would I do this without an explicit SQL JOIN?

I have a model called Question and another one called Answer.
class Question(models.Model):
text = models.CharField(max_length=140)
class Answer(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
uncertain = models.BooleanField()
text = models.CharField(max_length=30)
Now I'd like to have a QuerySet questions which is equivalent to Question.objects.all() but including the currently logged in user's answers. I'm almost completely sure that this could be accomplished by an explicit JOIN in SQL, but I'm certain there is a better way to do this using Django's ORM.
So how would I do that?
EDIT: To clarify, I'd like to be able to loop through the QuerySet, e.g. for q in questions and then be able to see whether or not the user has answered it through something like q.answered which could be a boolean, or q.answer which could be the Answer object itself (or None).
Are you looking for something like this?
[ item.question for question in Answer.objects.filter(user=request.user) ]
Or perhaps you want to do a
questions = Question.objects.all()
for question in questions:
question.answer = question.answer_set.filter(user=request.user)
I guess I'm not understanding the question and which direction you want to go...
EDIT: Ok, based on your edit, perhaps the latter... you can see if it's an empty list or tack on a .count() and see if the value is greater than 0.
I think you're overthinking things. Essentially what you want to know (as far as I can tell - correct me if I'm wrong) is which questions have been answered by a given user, and which questions haven't (the complement). So:
user_answered_questions = set([answer.question for answer in user.answer_set.all()])
Then unanswered questions are everything else. So:
user_unanswered_questions = set(Question.objects.all()) - answered_questions
The point of doing it this way rather than uzi's solution is that you should use what you know sooner rather than later. If you filter the answer_set for every question by user, your initial query will be selecting every single answer and every single question in these tables. By starting from the known data (i.e. the user) we are selecting only answers and questions related to that user.
EDIT:
So if you then want to combine the two sets into a single list, there are a bunch of ways you could do this for instance by creating a list of tuples:
questions = [(q, q in user_answered_questions) for q in Question.objects.all()]
Then you can iterate over questions, retaining the order of the queryset:
for q in questions:
print "it is %s that this user answered the \"%s\" question" % (q[1], q[0])
It's not pretty but you get the point - you've already determined what you wanted to determine, it's just a matter of contorting it to whatever data structure you happen to need.
What is wrong with
Question.objects.filter(answer__user=current_user).select_related()