1

I am trying to perform this sql query in zend:

SELECT a.id AS aid, 
    a.assignment_name AS name, 
    IFNULL((a.id = sa.aid),'0') AS status,
    a.assignment_name AS assignment_name,
        c.class_name AS class_name,
    a.due_date AS due_date,
        a.post_date AS post_date,
       (a.id = sa.aid) AS status,
       IF(a.description="",'No description given',a.description) AS description,
       IF(a.grading="",'No grading policy',a.grading) AS policy,
       CONCAT(u.prefix,' ',u.lname) AS teacher
FROM `assignments` as a
JOIN classes AS c ON a.class_id = c.id
JOIN enrollments as e ON e.student_id = 9 AND e.approved = 1 AND c.id = e.class_id
JOIN users as u on u.id = a.teacher_id
LEFT JOIN student_assignments as sa ON a.id = sa.aid
group by a.id;

my zend code is this:

public function getAllStudent($count, $offset) {
        $result = Zend_Db_Table::getDefaultAdapter()->select()
                        ->from(array('a' => 'assignments'),
                                array(
                                    'id' => 'a.id',
                                    'assignment_name' => 'a.assignment_name',
                                    'class_name' => 'c.class_name',
                                    'due_date' => 'a.due_date',
                                    'post_date' => 'a.post_date',
                                    'status' => "(a.id = sa.aid)",
                                    'description' => 'IF(a.description="",\'No description given\',a.description)',
                                    'policy' => 'IF(a.grading="",\'No grading policy\',a.grading)',
                                    'teacher' => 'CONCAT(u.prefix,\' \',u.lname)'
                        ))

                        ->join(array('c' => 'classes'), 'a.class_id = c.id', array())
                        ->join(array('e' => 'enrollments'), 'e.student_id = ' . $this->auth->id . ' AND e.approved = 1 AND c.id = e.class_id', array())
                        ->join(array('u' => 'users'), 'u.id = a.teacher_id', array())
                        ->joinLeft(array('sa' => 'student_assignments'), 'a.id = sa.id', array())
                        ->group('a.id')
                        ->order('a.due_date DESC')
                        ->limit($count, $offset)
                        ->query()
                        ->fetchAll();
        return $result;
    }

I am calling this from my controller as follows:

$this->view->assignments = $this->assignments->getAllStudent(20, 0);

When i perform this query in navicat (the first code paragraph) i get these results:

ID         name  !STATUS!   assignment_name  class   date              etc                      etc
24  Kill Animal 0   Kill Animal biology 2011-01-27  2011-01-26      You must kill a frog    Instant death = 100points. Slow death = 40points    Mr. Mihai
25  Solve Exercies  1   Solve Exercies  Math    2011-01-31  2011-01-26  1   You must solve all exercises    All exercises = 100points   Mr. Mihai

All is fine, that is the result that i want to get. Look at STATUS column (i've put !! to emphasize it). For one result STATUS = 0 and for the other STATUS = 1. This is what it should return. However if i perform the same query using the above code (second code paragraph) i get the following:

array(2) {
  [0]=>
  array(9) {
    ["id"]=>
    string(2) "25"
    ["assignment_name"]=>
    string(14) "Solve Exercies"
    ["class_name"]=>
    string(4) "Math"
    ["due_date"]=>
    string(10) "2011-01-31"
    ["post_date"]=>
    string(10) "2011-01-26"
    ["status"]=>
    NULL
    ["description"]=>
    string(28) "You must solve all exercises"
    ["policy"]=>
    string(25) "All exercises = 100points"
    ["teacher"]=>
    string(9) "Mr. Mihai"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(2) "24"
    ["assignment_name"]=>
    string(11) "Kill Animal"
    ["class_name"]=>
    string(7) "biology"
    ["due_date"]=>
    string(10) "2011-01-27"
    ["post_date"]=>
    string(10) "2011-01-26"
    ["status"]=>
    NULL
    ["description"]=>
    string(20) "You must kill a frog"
    ["policy"]=>
    string(48) "Instant death = 100points. Slow death = 40points"
    ["teacher"]=>
    string(9) "Mr. Mihai"
  }
}

Here is what $result->__toString returns in zend for the query:

 SELECT `a`.`id`, 
    `a`.`assignment_name`, 
    `c`.`class_name`, 
    `a`.`due_date`, 
    `a`.`post_date`,
     (a.id = sa.aid) AS `status`, 
    IF(a.description="",'No description given',a.description) AS `description`, 
    IF(a.grading="",'No grading policy',a.grading) AS `policy`, 
    CONCAT(u.prefix,' ',u.lname) AS `teacher` FROM `assignments` AS `a`
INNER JOIN `classes` AS `c` ON a.class_id = c.id
INNER JOIN `enrollments` AS `e` ON e.student_id = 9 AND e.approved = 1 AND c.id = e.class_id
INNER JOIN `users` AS `u` ON u.id = a.teacher_id
LEFT JOIN `student_assignments` AS `sa` ON a.id = sa.id 
GROUP BY `a`.`id` 
ORDER BY `a`.`due_date` 
DESC LIMIT 20

As you can see, status is NULL here. Why is this happening?

0

1 Answer 1

1

Your Zend query is missing the IFNULL function call. It should be:

'status' => "IFNULL(a.id = sa.aid,0)",

Since you are using a LEFT JOIN, if there are no sa recrods, sa.aid will be null, and any number = NULL will return NULL.

Another point i noticed, In your query you are joining on a.id = sa.aid, but in the zend query you are joining on 'a.id = sa.id' (not the missing a).

Sign up to request clarification or add additional context in comments.

4 Comments

even though i do that, still getting 0 for all the results.
thank you man! thank you from the bottom of my heart! You have no idea how stupid i feel right now...all this because of a stupid typo...damn it!
@user253530 Ur welcome. It happends to the best of us... I would recommend you using a tool like WinMerge (which does a text compare) to compare these - or any other - 2 queries, that seam to be the same. It will save you time & headaches.
thank for the tip, will definitely look for it as this is the most annoying thing that can happen when coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.