I've been trying to do a regular query with an inner join in Doctrine's DQL but with little success:
SQL:
SELECT
r.rtsRate
FROM tmc_rates_tbl_rts as r
INNER JOIN tmc_business_rel_buz as b ON (b.buzPcsID = r.rtsBuzID AND b.buzCmrID = r.rtsCmrID)
WHERE b.buzID = :business
AND r.rtsProID = :user;
DQL:
$qb = $this->rateRepo->createQueryBuilder('r')
->select('r.rate')
->innerJoin('r.business', 'business')
->where('r.business = :bid')
->andWhere('r.user = :user')
->setParameter('bid', $time->getBusiness())
->setParameter('user', $this->user)
->getQuery()
->execute();
Doctrine cames out with a double join out of the blue:
An exception occurred while executing 'SELECT t0_.rtsRate AS rtsRate_0 FROM tmc_rates_tbl_rts t0_ INNER JOIN rate_business r2_ ON t0_.id = r2_.rate_id INNER JOIN tmc_business_rel_buz t1_ ON t1_.id = r2_.business_id AND (t1_.buzStatus = 1) WHERE t1_.buzID = ? AND t0_.rtsProID = ?' with params [9, 5]:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tm_salesn.rate_business' doesn't exist
500 Internal Server Error - TableNotFoundException
I very much appreciate any help
Related
I have join many tables in this query via LEFT JOIN and i want to apply condition on table but php is giving me following error
Error Number: 1054 Unknown column '2021-08-23' in 'on clause'
SELECT i.isn_Add_Date, s.scentre_Name, i.isn_Job_No,cp.cpart_Part_Id,
cp.cpart_Part,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount,
p.product_Name,m.model_Name,c.complaint_Job_No,c.complaint_Add_Date,
c.complaint_Under_Warranty,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount
FROM complaints as c LEFT JOIN complaint_parts as cp ON c.complaint_Id
= cp.cpart_Complaint_Id LEFT JOIN scentres as s ON s.scentre_Id = c.complaint_Scentre_Id LEFT JOIN products as p ON p.product_Id =
c.complaint_Product_Id LEFT JOIN models as m ON m.model_Id =
c.complaint_Model_Id LEFT JOIN isn as i ON i.isn_Complain_Number =
c.complaint_Job_No AND i.isn_Add_Date BETWEEN '2021-08-23' AND
'2021-08-26' WHERE c.complaint_Scentre_Id = '1' AND c.complaint_Status
= 'Delivered' AND c.complaint_Trash = 0
here is my php code
$this->db->select('i.isn_Add_Date,s.scentre_Name,i.isn_Job_No,cp.cpart_Part_Id,cp.cpart_Part,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount,p.product_Name,m.model_Name,c.complaint_Job_No,c.complaint_Add_Date,c.complaint_Under_Warranty,cp.cpart_Qty,cp.cpart_Rate,cp.cpart_Amount');
$this->db->join('complaint_parts as cp', 'c.complaint_Id = cp.cpart_Complaint_Id ', 'left');
$this->db->join('scentres as s', 's.scentre_Id = c.complaint_Scentre_Id', 'left');
$this->db->join('products as p', 'p.product_Id = c.complaint_Product_Id', 'left');
$this->db->join('models as m', 'm.model_Id = c.complaint_Model_Id', 'left');
$this->db->JOIN('isn as i', 'i.isn_Complain_Number = c.complaint_Job_No AND i.isn_Add_Date BETWEEN '.str_replace('/', '-', $fromDate).' AND '.str_replace('/', '-', $toDate).'', 'LEFT');
$this->db->where("c.complaint_Scentre_Id", $serviceCenterId[0]);
$this->db->where("c.complaint_Status",'Delivered');
$this->db->where("c.complaint_Trash", 0);
$query = $this->db->get("complaints as c");
change
`2021-08-23` AND `2021-08-26`
to
'2021-08-23' AND '2021-08-26'
as the ` character implies columns and not strings
the sql is ok
maybe the orm can not handle complex join condition
you can move the join condition 【AND i.isn_Add_Date BETWEEN '2021-08-23' AND '2021-08-26'】 to where condition
just like $this->db->where("i.isn_Add_Date", BETWEEN ,['2021-08-23','2021-08-26]);
I need help on a particular codeigniter 3.1.6 active record query that keeps returning an error.
The code itself is:
$this->db->select('ms_payments.id, ms_payments.user_id, ms_payments.memberships_user_id, ms_payments.staff_id, ms_payments.rate_amount, ms_payments.tax, ms_payments.coupon, ms_payments.total, ms_payments.currency, ms_payments.pp, ms_payments.date,
u1.first_name AS user_name, u1.last_name AS user_last_name, u2.first_name AS staff_name, u2.last_name AS staff_last_name,
ms_gateways.displayname AS pp, ms_memberships_users.id, ms_memberships.title AS membership')
->from('ms_payments')
->join('auth_users as u1', 'u1.id = ms_payments.user_id')
->join('auth_users as u2', 'u2.id = ms_payments.staff_id')
->join($this->gTable, 'ms_gateways.id = ms_payments.pp')
->join($this->muTable, 'ms_memberships_users.id = ms_payments.memberships_user_id')
->join($this->mTable, 'ms_memberships.id = ms_memberships_users.membership_id')
->where('ms_payments.box_id =', $this->box_id);
$result = $this->db->get()->result();
The issue is related to the first two joins, I suspect is because the table name aliases.
The SQL query generated by $this->db->get_compiled_select() query works perfectly:
SELECT `ms_payments`.`id`, `ms_payments`.`user_id`, `ms_payments`.`memberships_user_id`, `ms_payments`.`staff_id`, `ms_payments`.`rate_amount`, `ms_payments`.`tax`, `ms_payments`.`coupon`, `ms_payments`.`total`, `ms_payments`.`currency`, `ms_payments`.`pp`, `ms_payments`.`date`,
`u1`.`first_name` AS `user_name`, `u1`.`last_name` AS `user_last_name`, `u2`.`first_name` AS `staff_name`, `u2`.`last_name` AS `staff_last_name`, `ms_gateways`.`displayname` AS `pp`,
`ms_memberships_users`.`id`, `ms_memberships`.`title` AS `membership`
FROM `ms_payments`
JOIN `auth_users` as `u1` ON `u1`.`id` = `ms_payments`.`user_id`
JOIN `auth_users` as `u2` ON `u2`.`id` = `ms_payments`.`staff_id`
JOIN `ms_gateways` ON `ms_gateways`.`id` = `ms_payments`.`pp`
JOIN `ms_memberships_users` ON `ms_memberships_users`.`id` = `ms_payments`.`memberships_user_id`
JOIN `ms_memberships` ON `ms_memberships`.`id` = `ms_memberships_users`.`membership_id`
WHERE `ms_payments`.`box_id` = '1'
and yet I still get this codeigniter error, any clues why?
Blockquote An uncaught Exception was encountered
Blockquote Type: Error
Blockquote Message: Call to a member function result() on boolean
I need to run following statements by CDbCriteria in Yii :
SELECT `tbl_products`.`id` FROM `tbl_products`
INNER JOIN `tbl_producttags`
ON `tbl_products`.`id` = `tbl_producttags`.`product_id`
INNER JOIN `tbl_tags`
ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`
What I've tried so far :
$criteria = new CDbCriteria();
$criteria->select= '`tbl_products`.`id`';
$criteria->join ='INNER JOIN `tbl_producttags` ON `tbl_products`.`id` = `tbl_producttags`.`product_id`'
. ' INNER JOIN `tbl_tags` ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`';
$products = Products::model()->findAll($criteria);
But it get me following error :
SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'tbl_products.id' in 'field list'. The SQL statement executed was:
SELECT tbl_products.id FROM tbl_products t INNER JOIN
tbl_producttags ON tbl_products.id =
tbl_producttags.product_id INNER JOIN tbl_tags ON
tbl_producttags.tag_id = tbl_tags.id
The problem is :
It's because of that CDbCriteria added unwanted alias name t after tbl_products
How can I fix it?
Use this
$criteria = new CDbCriteria();
$criteria->select= '`yourAlias`.`id`';
$criteria->alias="yourAlias";
$criteria->join ='INNER JOIN `tbl_producttags` ON `tbl_products`.`id` = `tbl_producttags`.`product_id`'
. ' INNER JOIN `tbl_tags` ON `tbl_producttags`.`tag_id` = `tbl_tags`.`id`';
$products = Products::model()->findAll($criteria);
the alias name of the table. If not set, it means the alias is 't'
In Symfony2, I have a many:many relationship between users and roles.
I am trying to get a list of all the users which are not linked to the ROLE_SUPER_ADMIN role.
Before migrating to Symfony2/Doctrine, I had achieved this with a simple NOT IN sql query, but for the life of me I can't achieve the same effect with doctrine.
Here is what I am trying:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb2 = $qb;
$dql = $qb->select('sa.id')
->from('AcmeAdminBundle:User', 'sa')
->leftJoin('sa.roles', 'r')
->andWhere('r.role = :role')
->getDQL();
$result = $qb2->select('u')
->from('AcmeAdminBundle:User', 'u')
->where($qb2->expr()->notIn('u.id', $dql))
->setParameter('role', 'ROLE_SUPER_ADMIN')
$users = $result->getQuery()->getResult();
But this is the error:
[Semantical Error] line 0, col 140 near 'sa LEFT JOIN':
Error: 'sa' is already defined.
And this is the output:
SELECT u
FROM AcmeAdminBundle:User sa
LEFT JOIN sa.roles r, AcmeAdminBundle:User u
WHERE u.id NOT IN (
SELECT sa.id
FROM AcmeAdminBundle:User sa
LEFT JOIN sa.roles r
WHERE r.role = :role
)
No idea why it is outputting like that as it should not be performing LEFT JOIN two times, my suspicion is that it has something to do with having two QueryBuilder instances, but could be something else entirely.
You need the MEMBER OF or in your case NOT MEMBER OF option.
$qb->select('sa.id')
->from('AcmeAdminBundle:User', 'sa')
->where(":Role NOT MEMBER OF sa.roles")
->setParameter("Role", <<ROLE_ID_OR_ROLE_ENTITY>>);
I didn't test this code, but it should give you some idea.
Full documentation can be found at http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html
I am trying to write a query in yii. I have the following which works
$criteria = new CDbCriteria;
$criteria->condition = "($column = :id)";
$criteria->params = array(":id" => $id );
$rows = Jobs::model()->with('pROJ')->findAll($criteria);
This returns the model of Jobs in array. I need to write the following query in yii to return a model
SELECT jobs.JOBNO, jobs.STATUS, projects.ORDERNO, jobs.PROJID, jobs.NAME, jobs.SEQ, jobs.PCENTDONE, jobs.EARNED, jobs.VALUE, jobs.DATEIN, jobs.DATEDONE, jobs.DATEDUE, jobs.SENTBACK, jobs.ORIGTAPES, jobs.COMMENTS, projects.CATEGORY, orders.BIDNO
FROM (jobs INNER JOIN projects ON jobs.PROJID = projects.PROJID) INNER JOIN orders ON projects.ORDERNO = orders.ORDERNO
where jobs.projid = 3002001
ORDER BY jobs.JOBNO, jobs.PROJID
I have tried the following but it does not work
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.ORDERNO, projects.CATEGORY, orders.BIDNO')
->from('jobs, projects, orders')
->join('projects p','jobs.PROJID = p.PROJID')
->join('orders o', 'p.ORDERNO = o.ORDERNO')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
I get the following error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.PROJID' in 'on clause'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`ORDERNO`, `projects`.`CATEGORY`, `orders`.`BIDNO`
FROM `jobs`, `projects`, `orders`
JOIN `projects` `p` ON jobs.PROJID=p.PROJID
JOIN `orders` `o` ON p.ORDERNO=o.ORDERNO
WHERE jobs.projid=:id
I have updated to
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.orderno, projects.category, orders.bidno')
->from('jobs')
->join('projects p','jobs.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
but i still get the error. All columns in mysql are CAPS
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects.orderno' in 'field list'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`orderno`, `projects`.`category`, `orders`.`bidno`
FROM `jobs`
JOIN `projects` `p` ON jobs.projid = p.projid
JOIN `orders` `o` ON p.orderno = o.orderno
WHERE jobs.projid=:id
As #DCoder said: your select should now read select('jobs.*, p.orderno, p.category, o.bidno'). For consistency you should also alias jobs as follows
$rows = Yii::app()->db->createCommand()
->select('j.*, p.orderno, p.category, o.bidno')
->from('jobs j')
->join('projects p','j.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('j.projid=:id', array(':id'=>$id))
->order('j.jobno,j.projid')
->queryRow();
I think you should remove projects, orders from ->from('jobs, projects, orders') and may be you should lower the case of jobs.PROJID as your error message says that it couldn't find the column.