I'm trying to use ResultSetMappingBuilder to get data from Native query.
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = :id";
I have no idea how to build ResultSetMappingBuilder.
public function createResultSetMapping() {
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('Event', 'e');
return $rsm;
}
Thanks for help in advance.
So I've used row SQL:
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = $id";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
Related
Please help me to check my query. I have search a lot and I have'nt try to select 3 tables before.
I think I got it right but I dont know why there's nothing happen.
public function delSection($delete_id)
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt->execute(array(":del_id"=>$delete_id));
while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
{
unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
}
$stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt2->bindparam(":del_id",$delete_id);
$stmt2->execute();
return true;
}
What I am trying to do is to select * from 3 tables and fetch their data with fk sec_id
here's the manual running of query
link:
Code:
Done With LEFT OUTER JOIN QUERY
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:unlink_id");
Before you guys send me to something else because there is already something like this answered. I don't quite understand the examples given there.
function joinBestelling (){
$sql =( "SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
so i have two tables now artikel and factuurregel how do i add like 3 more tables with info in the join ?
Simply join the outher tables like the first one:
SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode
INNER JOIN table3 on .....
INNER JOIN table4 on .....
Hopefully that was your question
function joinBestelling (){
$sql =( "SELECT factuurregel.factuurNummer, factuurregel.artikelCode, factuurregel.aantal, factuurregel.prijs, factuur.factuurNummer, factuur.factuurDatum, factuur.klantCode, klant.klantCode, klant.voorLetter, klant.achterNaam, artikel.artikelCode, artikel.artikel, artikel.prijs FROM factuurregel JOIN factuur ON factuurregel.factuurNummer = factuur.factuurNummer JOIN klant ON factuur.klantCode = klant.klantCode JOIN artikel ON factuurregel.artikelCode = artikel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
I have a question.
This is my query:
DROP VIEW IF EXISTS Bedrijven_speci;
CREATE VIEW Bedrijven_speci as SELECT bedrijfgegevens.id,
bedrijfgegevens.subbranche_id ,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10;
SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
$query;
$sth = $pdo->prepare($query);
$sth->execute();
$fetch = $sth->fetchAll();
It works when I execute the query in Phpmyadmin. But when I execute in with PDO it returns an empty array. Can someone help me?
Just execute the statements one after another.
If required start a transaction before executing the the statements via PDO::beginTransaction
<?php
$queries = array(
'DROP VIEW IF EXISTS Bedrijven_speci',
'CREATE VIEW Bedrijven_speci as
SELECT
bedrijfgegevens.id,bedrijfgegevens.subbranche_id,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10',
'SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id'
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
try {
foreach( $queries as $q ) {
$pdo->exec($q);
}
}
catch(Exception $ex) {
$pdo->rollBack();
throw $ex;
}
$pdo->commit();
I have used following code for simple join in Doctriner in Codeigniter:
$query = $this->em->createQuery('SELECT u.name,u.subject,pds.subject_name FROM PdContact u,PdSubject pds WHERE pds.id=u.sub_id');
But how to do left join? When I tried to to left join it shows error.
Please suggest solution.
You can use this :
$sql = "SELECT u.name,u.subject,pds.subject_name FROM PdContact u LEFT JOIN PdSubject pds ON pds.id=u.sub_id;";
$connection = $this->em->getConnection();
$cleanreq = $connection->prepare($sql);
$cleanreq->execute();
$result = $cleanreq->fetchAll();
return $result;
For more infos on Joining you can check this doc : http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
I have this query call:
$currplayer = 1;
$query = "SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f
INNER JOIN ".$prefix."_users u
ON f.fk_user_id2 = u.id
WHERE f.fk_user_id1 = $currplayer ORDER BY u.username ASC";
if ($result = $mysqli->query($query)){
while ($row1 = $result->fetch_assoc()){
$friendid = $row1['fk_user_id2'];
$sql = $mysqli->query("SELECT * FROM ".$prefix."_newversus WHERE fk_player=$currplayer AND fk_opponent=$friendid");
$row = $sql->fetch_assoc();
I am trying to figure out how I get the query within the loop into the first query with inner join, but can't figure this out.
hoping for help and thanks in advance :-)
If i got you corrrectly, you want to remove the query which is running in loop & add it to main query.
Try this:
SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f
INNER JOIN ".$prefix."_users u ON f.fk_user_id2 = u.id
INNER JOIN ".$prefix."_newversus n ON f.fk_user_id2 = n.fk_opponent
WHERE f.fk_user_id1 = $currplayer AND
n.fk_player=$currplayer
ORDER BY u.username ASC;