Select query in Symfony error - php

I am getting the below error while fetching data from table.
500 | Internal Server Error | Doctrine_Exception
Couldn't find class tasks_comments
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasks_comments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id']);
$results1 = $r->execute();
But, I didn't get any error for another table as the above format. please check and suggest me.

change this
->where('u.tasks_id = ?', $arr_values['tasks_id'])
to
->andwhere('u.tasks_id = ?', $arr_values['tasks_id'])

Related

SQLSTATE[42000]: Syntax error or access violation: 1064 laravel

In my Laravel code I use DB to access to database. In controller function named showTeacherList I am trying to get all works of every teacher. But Laravel returns this error. When I dump query, copy past it to phpmyadmin and run, it works. My code:
function showTeacherList($login){
$select_result = DB::select('select id, name from kafedra where login = ?', [$login]);
$kafedra_id = $select_result[0]->id;
$teachers = DB::select('select id, name from teacher where kafedra_id = ?', [$kafedra_id]);
$teacher_works = [];
$i = 0;
foreach ($teachers as $teacher) {
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?' [$teacher->id]);
$i++;
}
return view('kafedra/teacher_list', ['login' => $login, 'name' => $select_result[0]->name, 'teachers' => $teachers, 'teacher_works' => $teacher_works]);
}
Please help, thanks
You have a syntax error in your DB::select statement, specifically within your foreach loop. You've missed a comma separating your query and bindings.
Replace:
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?' [$teacher->id]);
With:
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?', [$teacher->id]);

Laravel SQL Query Keeps Giving SQLSTATE[42000]: Syntax error or access violation: 1064

I have this SQL query in my controller at laravel
$distinct_course = DB::table('student')
->select(DB::raw('count(*) as grad_count, `student_course`, MONTH(`student_date_ended`)'))
->where('student_course', '=', 'Basic Computer')
->whereYear('student_date_ended', '=', '2015')
->groupby(DB::raw('MONTH(`student_date_ended`'))
->get();
Which is based on this SQL query I made to work first before converting it to Laravel
select count(*) as grad_count, `student_course`, MONTH(`student_date_ended`) from `student` where `student_course` = "Basic Computer" and year(`student_date_ended`) = 2015 group by MONTH(`student_date_ended`)
But for some reason I always get this error.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (SQL: select count(*) as grad_count, student_course, MONTH(student_date_ended) from student where student_course = Basic Computer and year(student_date_ended) = 2015 group by MONTH(student_date_ended)
Am I doing something wrong here that I'm not aware of?
As an alternative, in all my projects I use the following way of DB interactions:
$Query = 'select count(*) as grad_count, `student_course`,
MONTH(`student_date_ended`)
from `student`
where `student_course` = ?
and year(`student_date_ended`) = ?
group by MONTH(`student_date_ended`)';
$GradStudents = DB::select($Query, [ $StudenCourse, $StudentDateEnded]);
This way I can go from confirmed working MySQL statement to Laravel without worrying of call not working after conversion.
Note: Instead of keeping your db calls in the controller, I would them to a model folder. This way you will maintain the MVC design pattern.
With Paul's help I managed to get it working with this code.
$Query = 'select count(*) as grad_count, `student_course`,
MONTH(`student_date_ended`) as Month
from `student`
where `student_course` = ?
and year(`student_date_ended`) = ?
group by MONTH(`student_date_ended`), student_course';
$StudentCourse = 'Basic Computer';
$StudentDateEnded = 2015;
$distinct_course = DB::select($Query, [ $StudentCourse, $StudentDateEnded]);
It would seem that part of the problem was I also needed to add the 'student_course' column in the group by so I tried adding it to my original Laravel code and also managed to get it work with this manner.
$distinct_course = DB::table('student')
->where('student_course', '=', 'Basic Computer')
->whereYear('student_date_ended', '=', '2015')
->select(DB::raw('count(*) as grad_count, `student_course`, MONTH(`student_date_ended`) as Month'))
->groupby(DB::raw('MONTH(`student_date_ended`), student_course'))
->get();

Codeigniter Active Record SQL syntax error

I have the following Active Record pattern within one of my models :
$this->db->get('names');
$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');
$query = $this->db->get();
This gives me a syntax error :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio' at line 2
The full statement being returned is :
SELECT * WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio` desc
I don't know why my table names isn't showing as I'm calling $this->db->get('names'); which should produce SELECT * FROM names, is it just not returning in the error? What's wrong with this statement and what should I do to correct my Active Record call?
get needs to go at the end. If you want to select a table before - use $this->db->from('names'); and then just $this->db->get();. So the full code:
$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');
$query = $this->db->get('names');
or the chaining version:
$query = $this->db->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get('names');
Using from:
$query = $this->db->from('names')
->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get();

Codeigniter active record : how to read the joined table

i have some table, and the relationship goes like this
and i want to get all the record of those 2 tables. so i use this query in my model
$this->db->select('*');
$this->db->from('ms_Kategori_Material.*,ms_Material_Jasa.*');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
and then i got error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode' at line 2
SELECT * FROM (`ms_Kategori_Material`.*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode_Kategori_Material_Jasa` = `ms_Material_Jasa`.`Kode_Kategori_Material_Jasa`
why do i can't read the needed table ?
in your from, there shouldnt be a .*,
i.e
$this->db->from('ms_Kategori_Material','ms_Material_Jasa');
and if your adding the table to join, no need to add it to from.
so it becomes
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa','....');
final query:
$this->db->select('*');
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
You may try this (to join and select fields from both tables)
$this->db->select('ms_Kategori_Material.*, ms_Material_Jasa.*');
$this->db->from('ms_Kategori_Material');
$this->db->from('ms_Material_Jasa');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$table = $this->db->get();
return $table->result();
$this->db->select('*');
$this->db->from('ms_Kategori_Material'); // full table name
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
//print_r($this->db->last_query()); display raw sql
$print_r($result->result_array());
Read more # http://codeigniter.com/user_guide/database/active_record.html

Using subqueries with doctrine / Errors with aliases

I'm trying to make a simple query with a subquery in a orWhere clause (with Doctrine).
As always, Doctrine tries to rename every aliases and completely destroys the queries...
Here's an example:
$q = Doctrine_Query::create()
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie as cat where cat.categorie_id =?)', $id)
->execute();
Which in MySQL would make something like:
SELECT *
FROM actualite a
WHERE a.categorie_id = 1 OR a.categorie_id IN (SELECT cat.id FROM categorie cat WHERE cat.categorie_id = 1);
Everything is right about it, but then again Doctrine destroys it:
Couldn't find class cat
Every time I try to do something a little complex with Doctrine, I have errors with aliases. Any advice or ideas about how to fix this?
Thanks!
The SQL example you've provided is fine but the corresponding Doctrine syntax has a couple of errors. Here's a clean version:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
->orWhere('a.categorie_id IN (SELECT cat.id FROM Categorie cat WHERE cat.categorie_id = ?)', $id)
->execute();
You should use createSubquery() to explicitely tell doctrine about your nested subquery. So your query should look something like this:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
;
$subquery = $q->createSubquery()
->select("cat.id")
->from("Categorie cat")
->where("cat.categorie_id = ?", $id)
;
$q->orWhere('a.categorie_id IN ('.$subquery->getDql().')')->execute();
Another example can be found here:
http://www.philipphoffmann.de/2012/08/taming-doctrine-subqueries/
I think you query should be like this add the select and remove as
$q = Doctrine_Query::create()
->select('a.id')
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie cat where cat.categorie_id =?)', $id)
->execute();
Try this may help you.
Thanks

Categories