I am trying to run this query:
$query = $this->getEntityManager()
->createQuery(
'SELECT MDPIBackendBundle:Articles
FROM MDPIBackendBundle:Articles art
LEFT JOIN MDPIBackendBundle:ScopusFTPUploads uploaded_art WITH art.id = uploaded_art.article_id
WHERE uploaded_art.article_id IS NULL AND art.pubdate_published >= "'.$startDate.'" AND art.pubdate_published < "'.$endDate.'"'
);
And I am geting this error:
[Syntax Error] line 0, col 272: Error: Expected Literal, got '"'
Do you know where is the problem? Thank you.
It seems like a typo, you cant use " in sql/dql. Try:
$query = $this->getEntityManager()
->createQuery(
"SELECT MDPIBackendBundle:Articles
FROM MDPIBackendBundle:Articles art
LEFT JOIN MDPIBackendBundle:ScopusFTPUploads uploaded_art WITH art.id = uploaded_art.article_id
WHERE uploaded_art.article_id IS NULL AND art.pubdate_published >= '".$startDate."' AND art.pubdate_published < '".$endDate."');
Related
SQL:
$datas = DB::select("SELECT a.id,a.parent_id, d.action_id, d.role_id,d.department_id, d.is_created,
d.is_edit,d.is_delete,d.is_view,d.is_audit,d.is_verify,d.is_approved
FROM application_security_actions a
LEFT JOIN application_default_permissions d on d.action_id = a.id where d.role_id = " . $id . " or d.role_id is null");
Query Builder:
$datas = DB::select('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();
sql is working perfectly but when is convert in to query builder it is not working. can any on check where i am wrong
Change on the line 1
DB::select to DB::table
Full code:
$datas = DB::table('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();
What is the correct/efficient way to display results based on a variable if the variable is set? If variable is not set, the AND operator should not be used.
I apologize if this is a repeat, I clicked the suggested links and they did not make sense to me.
Near the end of code is my note with ^^^^^ marked.
For example:
$whatever = 123;
SELECT
DISTINCT terms.name as product_type_name,
tax.term_id as termidouter
FROM $wpdb->posts AS p
INNER JOIN wp_term_relationships r
ON p.ID = r.object_id
INNER JOIN wp_term_taxonomy tax
ON r.term_taxonomy_id = tax.term_taxonomy_id
INNER JOIN wp_terms terms
ON tax.term_id = terms.term_id
WHERE
tax.taxonomy = 'product_type'
AND p.post_status = 'publish'
AND p.post_type = 'product'
AND '$whatever ' = terms.term_id
^^^^ If $whatever is empty, I want to return results as if this line did not exist.
ORDER BY product_type_name
");
I was going to do an IF/ELSE but I figured that was the lazy way.
$whatever = 123;
if (empty($whatever)) {
// SELECT without the AND
} else {
// SELECT with the AND
}
You could do something like this:
AND CASE WHEN '$whatever ' IS NOT NULL THEN '$whatever ' ELSE terms.term_id END = terms.term_id
So... typically you'd want to use prepared statements, but if we're going this route, I would collect all my optional search criteria in an array as strings:
$myTerms=array();
if(!empty($whatever)) {
$myTerms[]="terms.term_id='" . $whatever . "'";
}
...
Then you can build your query easily like so:
$mySql = "SELECT * FROM whatever WHERE somefield='somevalue' ";
if(count($myTerms)>0) {
$mySql.=" AND " . implode(" AND ", $myTerms);
}
Please note that this is a basic example; you should also be checking any value you pass into your query for attacks etc.
Can you please convert it to DQL :
SELECT molecule.cas, molecule.id_molecule, molecule.statutvlep8h, statutvlepct,
vlep8h_mg, vlepct_mg,molecule.unitevlep, prelevement.id_laboratoire
FROM thym_dev.molecule
INNER JOIN thym_dev.prelevement
WHERE molecule.id_molecule = prelevement.id_molecule
UNION ALL
SELECT molecule.cas, molecule.id_molecule, molecule.statutvlep8h, statutvlepct,
vlep8h_mg, vlepct_mg,molecule.unitevlep, analyse.id_laboratoire
FROM thym_dev.molecule
INNER JOIN thym_dev.analyse
WHERE molecule.id_molecule = analyse.id_molecule;
I get the answer :
$queryBuilder0 = "
SELECT molecule.molecule, molecule.cas, molecule.statutvlep8h,molecule.statutvlepct,molecule.vlep8hMg, molecule.vlepctMg,molecule.unitevlep,IDENTITY(prelevement.laboratoire)
FROM AppBundle:Molecule molecule
INNER JOIN AppBundle:Prelevement prelevement
WHERE prelevement.molecule= molecule.id
";
$queryBuilder1 = "
SELECT molecule.molecule , molecule.cas, molecule.statutvlep8h, molecule.statutvlepct, molecule.vlep8hMg, molecule.vlepctMg,molecule.unitevlep,IDENTITY(analyse.laboratoire)
FROM AppBundle:Molecule molecule
INNER JOIN AppBundle:Analyse analyse
WHERE analyse.molecule= molecule.id
";
$results = array_merge($this->_em->createQuery($queryBuilder0)->getResult(), $this->_em->createQuery($queryBuilder1)->getResult());
I want to try change my sql code to active record, here is the original code and active record try. And the error message. Please tell me where is my wrong? Thanks.
SELECT `emlakilan`.`id`, `emlakdurum`.`durum`,`emlaktip`.`tip`,`semtler`.`semt`,`emlakilan`.`fiyat`,`emlakilan`.`tarih`,`resim`.`r1` FROM ucburcak.`emlakilan`
INNER JOIN `semtler` ON `emlakilan`.`semtId` = `semtler`.`semtid`
INNER JOIN `emlaktip` ON `emlakilan`.`emlakTipId` = `emlaktip`.`id`
INNER JOIN `emlakdurum` ON `emlakilan`.`emlakDurumId` =`emlakdurum`.`id`
LEFT JOIN `resim` ON `emlakilan`.`resimId` = `resim`.`id`
WHERE `emlakdurumId`=3 ORDER BY `emlakilan`.`id` DESC LIMIT 4;
//and here active record function
public function kucuk_ilan($durum ='3') // $durum:1=>tümü 2=>kiralık 3=>satılık 4=>takas
{
$query = $this->db->select('emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1')
->from('emlakilan')
->join('semtler','emlakilan.semtId' === 'semtler.semtid','inner')
->join('emlaktip','emlakilan.emlakTipId'==='emlaktip.id','inner')
->join('emlakdurum','emlakilan.emlakDurumId'==='emlakdurum.id','inner')
->join('resim','emlakilan.resimId'==='resim.id','left')
->where('emlakdurumId'===$durum)
->order_by('emlakilan.id','desc')
->limit(4);
$query = $this->db->get('emlakilan');
return $query->result_array();
}
And, error message:
Error Number: 1066
Not unique table/alias: 'emlakilan'
SELECT emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1 FROM (emlakilan, emlakilan) INNER JOIN semtler ON INNER JOIN emlaktip ON INNER JOIN emlakdurum ON LEFT JOIN resim ON WHERE 0 IS NULL ORDER BY emlakilan.id desc LIMIT 4
Filename: C:\Program Files\EasyPHP-12.1\www\3burcak\system\database\DB_driver.php
Line Number: 330
Thanks for help.
So 1. remove the 'from' line
2. the joins where wrong
try this:
public function kucuk_ilan($durum ='3') // $durum:1=>tümü 2=>kiralık 3=>satılık 4=>takas
{
$query = $this->db->select('emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1')
->from('emlakilan')
->join('semtler','emlakilan.semtId = semtler.semtid','inner')
->join('emlaktip','emlakilan.emlakTipId = emlaktip.id','inner')
->join('emlakdurum','emlakilan.emlakDurumId = emlakdurum.id','inner')
->join('resim','emlakilan.resimId = resim.id','left')
->where('emlakdurumId', $durum)
->order_by('emlakilan.id','desc')
->limit(4);
$query = $this->db->get('emlakilan');
return $query->result_array();
}
I´m working with Symfony2 and I need to execute this SQL for example:
select detformacion.* from detformacion
left join formacion
on detformacion.formacion_id = formacion.id
left join detcurso
on formacion.id = detcurso.formacion_id
where detcurso.id IN ('143','144');
For that, I have this in my Repository:
public function getSeleccion() {
$em = $this->getEntityManager();
$query = $em->createQueryBuilder()
->select('d')
->from('GitekUdaBundle:Detformacion', 'd')
->leftJoin('d.formacion', 'f')
->leftJoin('f.detcursos', 'det')
->where('det.id = :miarray')
->setParameter('miarray',array('143','144'))
->getQuery()
;
return $query->getResult();
}
I tried with ->where('det.id IN :miarray') but I´m getting errors all the time.
Any help or clue?
thanks in advance.
UPDATE: The problem is setting the parameters.
Missing parentheses after the IN operator :
->where('det.id IN (:miarray)')
->setParameter('miarray', array('143','144'))