Update query with multiple where conditions - symfony

I am trying to update the row. Here I have multiple where conditions. This is the query I have tried.
$this->createQueryBuilder("jv")
->update()
->set("jv.pictureStatus", "?1")
->set("jv.pictureStatusId", "?2")
->andWhere("jv.aa = ?3")
->andWhere("jv.bb = ?4")
->andWhere("jv.cc = ?5")
->andWhere("jv.dd = ?6")
->andWhere("jv.ee = ?7")
->andWhere("jv.ff = ?8")
->andWhere("jv.gg = ?9")
->setParameter(1, "100")
->setParameter(2, "200")
->setParameter(3, $wheres["aa"])
->setParameter(4, $wheres["bb"])
->setParameter(5, $wheres["cc"])
->setParameter(6, $wheres["dd"])
->setParameter(7, $wheres["ee"])
->setParameter(8, $wheres["ff"])
->setParameter(9, $wheres["gg"])
->getQuery()->execute();
When I execute this query it returns 0 but nothing is getting updated.

Related

how to make this in doctrine2

I need to make a sql query like this,
UPDATE org_mapping SET is_active = 1 WHERE (org_id = ? AND service_provider_id = ? )OR (org_id = ? AND service_provider_id = ?)
I tried this but its now working:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where('om.organization = ?1')->andWhere('om.serviceProvider = ?2')
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
When i am running, i am getting the following query:
UPDATE org_mapping SET is_active = 1 WHERE ((org_id = ? AND service_provider_id = ?) OR org_id = ?) AND service_provider_id = ?
Replace
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
With
->orWhere('om.organization = ?2 AND om.serviceProvider = ?1')
Try this:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where(
$qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?1')
,
$qb->expr()->eq('om.serviceProvider','?2')
),
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?2')
,
$qb->expr()->eq('om.serviceProvider','?1')
)
)
)
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
You should not do it.
If you do an UPDATE query, you're missing the whole point of using Doctrine, that is mapping objects to database rows, not just an abstraction to queries.
Instead, extract your objects, then do a cycle and perform actions on single objects, then flush after the cycle.

Oracle Stored Procedure unable to call from ASP.NET

I have written a code in ASP.NET to fetch data from Oracle stored procedure. But I get an error when trying to fetch the data:
ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'USER_FEEDBACK' ORA-06550: line 1, column 7: PL/SQL: Statement ignored
Procedure code is this:
create or replace PROCEDURE user_feedback(cv_results out sys_refcursor,start_date IN VARCHAR2)
IS
BEGIN
open cv_results for
select pi.first_name || ' ' || pi.last_name initiator
......
from request_workflow w inner join request_workflow_attribute waRating
on waRating.request_workflow_id = w.row_id
and waRating.attr_name = 'UserRating'
.............
where w.date_stamp_utc between start_date and '31-dec-2015'
order by waRating.attr_value desc, eform_name;
END ;
This is my ASP.NET code:
Oracle.DataAccess.Client.OracleCommand objCmd = new Oracle.DataAccess.Client.OracleCommand();
objCmd.Connection = objConn;
objCmd.CommandText = "user_feedback";
objCmd.CommandType = CommandType.StoredProcedure;
Oracle.DataAccess.Client.OracleParameter oraP = new Oracle.DataAccess.Client.OracleParameter();
// System.Data.OracleClient.OracleParameter op = null;
oraP.OracleDbType = OracleDbType.RefCursor;
oraP.ParameterName = "cv_results";
oraP.Direction = System.Data.ParameterDirection.Output;
oraP.ParameterName = "start_date";
oraP.OracleDbType = OracleDbType.Varchar2;
oraP.Value = "01-Dec-2015";
oraP.Direction = System.Data.ParameterDirection.Input;
objCmd.Parameters.Add(oraP);
objConn.Open();
objCmd.ExecuteReader();
objCmd.ExecuteNonQuery();
Please suggest how to make it work.
You're only creating/adding a single parameter but assigning it twice with different values resulting in just one parameter to the call.
Instead, create and add two separate parameters, something like;
var oraP1 = new Oracle.DataAccess.Client.OracleParameter();
oraP1.OracleDbType = OracleDbType.RefCursor;
oraP1.ParameterName = "cv_results";
oraP1.Direction = System.Data.ParameterDirection.Output;
objCmd.Parameters.Add(oraP1);
var oraP2 = new Oracle.DataAccess.Client.OracleParameter();
oraP2.ParameterName = "start_date";
oraP2.OracleDbType = OracleDbType.Varchar2;
oraP2.Value = "01-Dec-2015";
oraP2.Direction = System.Data.ParameterDirection.Input;
objCmd.Parameters.Add(oraP2);
It's probably because you don't Add the first parameter. Try something like this?
...
oraP.ParameterName = "cv_results";
oraP.Direction = System.Data.ParameterDirection.Output;
objCmd.Parameters.Add(oraP);
oraP = new Oracle.DataAccess.Client.OracleParameter();
oraP.ParameterName = "start_date";
...
Though, it'd be better to declare another variable for the first param if you'll be reading from that refcursor later.

doctrine dql exception: Illegal offset type in /var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 601

I want to produce a DQL for following MySQL query:
SELECT * FROM `folders` AS `t` WHERE `t`.`Library` = #myLib AND AND `t`.`Id` NOT IN (
SELECT DISTINCT(`f`.`Id`) FROM `folders` AS `f` JOIN `folders` AS `ff` ON (`f`.`Position` LIKE CONCAT(`ff`.`Position`, '%')) WHERE `ff`.`Active` = 1 AND `ff`.`Library` = #myLib AND `f`.`Library` = #myLib
)
ORDER BY `t`.`Position` ASC
The query works fine in mySQL and returns correct records.
To generate DQL I've tried both below options:
1.
$query = $em->createQuery("SELECT F FROM MyBundle:Folders T WHERE T.Library = :libid AND T.id NOT IN (
SELECT DISTINCT(F.id) FROM MyBundle:Folders F JOIN MyBundle:Folders FF WITH F.Position LIKE CONCAT(FF.Position, '%') AND F.Library = :libid AND FF.Library = :libid AND FF.Active = true
) ORDER BY T.Position ASC")
->setParameter('libid', $library);
$result = $query->getResult();
2.
$q1 = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$q1->join('\MyBundle\Entity\Folders', 'FF', 'WITH', $q1->expr()->like('F.Position', $q1->expr()->literal('CONCAT(FF.Position, \'%\')')))
->where('FF.Active = true')
->andWhere("FF.Library = '$library'")
->andWhere("F.Library = '$library'");
$q2 = $this->createQueryBuilder('T');
$q2->where('T.Library = :libid')
->andWhere($q2->expr()->notIn('T.id', $q1->getDQL()))
->setParameter('libid', $library)
->orderBy('T.Position', 'ASC');
$result = $q2->getQuery()->getResult();
In my perspective it seems OK but I don't know why in both ways it produce following exception:
ContextErrorException: Warning: Illegal offset type in
/var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 601
Any help will be appreciated.
It seems no one has an answer for this. I found a temporary solution as below (I call it temporary because I'm changing my unique query to two separate queries and it seems the issue is in core of doctrine).
$qb = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$qb->join('\MyBundle\Entity\Folders', 'FF', 'WITH', 'F.Position LIKE CONCAT(FF.Position, \'%\')')
->where('FF.Active = true')
->andWhere("FF.Library = :library")
->andWhere("F.Library = :library")
->setParameter('library', $library);
$included_folders = $qb->getQuery()->getArrayResult();
$query = $this->createQueryBuilder('F')
->where('F.Active = false')
->andWhere('F.Library = :library')
->setParameter('library', $library)
->orderBy('F.Position', 'ASC');
if (!empty($included_folders)) {
if (count($included_folders) > 1)
{
foreach ($included_folders as $index => $value)
{
if (is_array($value))
{
$included_folders[$index] = !empty($value['id']) ? $value['id'] : $value[1];
}
}
$query->andWhere($query->expr()->notIn('F.id', $included_folders));
}
else {
$query->andWhere('F.id != :folder ')
->setParameter('folder', $included_folders[0]);
}
}
$result = $query->getQuery()->getResult();
As you see instead of getting the dql from my first query and putting it inside my second dql in notIn section which will lead to the warning message, I execute the first query and get the results then put the results inside notIn if amount of returned values are more than one, otherwise it should be in regular !=. This solved my problem for now, but as you see amount of transactions are now increased
If anyone has a better solution or any fix for the warning I will be thankful.
I've encountered the same error and it seems that this has been fixed in latest trunk of Doctrine/ORM.
Using "2.5.*#dev" as version in your composer.json for doctrine/orm should fix this bug and will let you do what you want in a single query.

use ArrayList as filter in linq query "where" keyword

I have some data in an ArrayList and I would like to use that to filter my Linq query using the where clause.
My Linq code below joins two tables and then I filter them using the Where clause. Now I would like to FURTHER filter this query by using the Arraylist as a filter. So the value come from arraylist
I would like the "where" clause to take one more comparison and the value comes from an arraylist:
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue && rType.Field<string>("Name") == lbHWTypes.SelectedValue && **arrayList.Tostring()**
This is the code that I am using.
Can anyone tell me how can I further filter my Linq query using the values in the arraylist?
joined = from rType in ds.Tables["HWTypes"].AsEnumerable()
join rStock in ds.Tables["Stock"].AsEnumerable()
on rType.Field<string>("ProductID") equals rStock.Field<string>("Partno")
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue && rType.Field<string>("Name") == lbHWTypes.SelectedValue
select new
{
TagNumber = rStock.Field<string>("TagNumber"),
SerialNumber = rStock.Field<string>("SerialNumber"),
Partno = rStock.Field<string>("Partno"),
PartType = rStock.Field<string>("PartType"),
EcopartSubtype = rStock.Field<string>("EcopartSubtype"),
AzertyQuerty = rStock.Field<string>("Azerty/Querty"),
ProductID = rType.Field<string>("ProductID"),
Name = rType.Field<string>("Name"),
SCCMKeyboard = rType.Field<string>("SCCMKeyboard"),
DisplayName = rType.Field<string>("DisplayName"),
ProfSSCMName = rType.Field<string>("ProfSSCMName"),
TagNameDisplayName = rStock.Field<string>("TagNumber") + " " + rType.Field<string>("DisplayName")
// add the other columns you need here
};
You seem to be using Linq-To-Objects.
So you can just use contains on the arraylist
where rType.Field<string>("ProfSSCMName") == lbProfiles.SelectedValue
&& rType.Field<string>("Name") == lbHWTypes.SelectedValue
&& arrayList.Contains( rType.Field<string>("Name") )

innerjoin query in drupal 7

I am applying this query for below D6 query , not working ..dont know wat wrong i'm doing ....does innerjoin fails in some condition
$result = db_select('px_slides','s')
->join('node','n','s.vid = n.vid')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=')
->execute()->fetchObject();
drupal 6 query i have:
$result = db_query('
SELECT n.nid, n.vid, n.sticky, n.title, n.created, s.cid, s.ref_id, s.viewurl, s.specimen_type, s.tissue_type, s.body_site, s.test_type, s.algorithm, s.result
FROM {px_slides} s INNER JOIN {node} n ON n.vid = s.vid
WHERE n.status = 1 ')->execute();
You need to put your call to ->join() on a separate line altogether, as it doesn't return the query object:
$query = db_select('px_slides','s')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$query->join('node','n','s.vid = n.vid');
$result = $query->execute()->fetchObject();
The join method does not chain like that. You will have to do something like:
$query = db_select('px_slides','s')
->join('node','n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$result = $query->execute()->fetchObject();
Also you can use the to string magic method to see the query it is going to execute.
$query->__toString();
Try this...
$query = db_select('px_slides', 's');
$query->innerJoin('node,'n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'));
$query->fields('n',array('sticky','title'));
$query->condition('n.status','1');
$query->condition('s.cid','126');
$result= $query->execute()->fetchAll(PDO::FETCH_ASSOC);

Resources