Moinsen,
wir haben folgendes Problem:
Wir haben schon eine fertige SQL abfrage. Jetzt Tippern wir uns mittlerweile tot wie man diese nun in PHP schreiben kann:
select
s_articles_details.ordernumber as ArtikelNR,
s_articles_details.articleID as ArtikelID,
s_articles_details.instock as Warenbestand,
s_articles_details.ean as EAN,
(sum(s_articles_prices.price*s_core_tax.tax)/100+s_articles_prices.price) as VK
from
s_articles
left join s_articles_details on s_articles.ID=s_articles_details.articleID
left join s_articles_prices on s_articles_prices.articleID=s_articles.id
left join s_core_tax on s_core_tax.ID=s_articles.taxID
group by s_articles.id
Unser Versuch schaut so aus:
$connection->select(
‚s_articles_details.ordernumber as ArtikelNR‘,
‚s_articles_details.articleID as ArtikelID‘,
‚s_articles_details.instock as Warenbestand‘,
‚s_articles_details.ean as EAN‘,
‚(sum(s_articles_prices.price*s_core_tax.tax)/100+s_articles_prices.price) as VK‘)
->from(‚s_articles‘, ‚details‘)
->leftJoin(‚details‘ , ‚s_articles_details‘,‚aa‘,‚s_articles.ID=s_articles_details.articleID‘)
->leftJoin(‚details‘, ‚s_articles_prices‘ , ‚bb‘ , ‚s_articles_prices.articleID=s_articles.id‘)
->leftJoin(‚details‘, ‚s_core_tax‘ , ‚cc‘ , ‚s_core_tax.ID=s_articles.taxID‘)
->groupBy(‚ArtikelID‘);
Sobald wir die Joins einbauen, kommt folgender Fehler: Fatal error : Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‚s_articles_prices.price‘ in ‚field list‘ in /…
Der JOIN für s_articles vermisse ich, und auch groß und kleinschreibung.
IN PHP setze ich das so um:
$query = "SELECT
sad.ordernumber AS ARTIKELNR,
sad.articleID AS ARTIKELID,
sad.instock AS Warenbestand,
sad.ean AS ean,
ROUND((sap.price*sct.tax)/100+sap.price,2) AS VK
FROM s_articles_details AS sad
INNER JOIN s_articles AS sa ON sa.id = sad.articleID
INNER JOIN s_articles_prices AS sap ON sap.articleID = sa.id
INNER JOIN s_core_tax AS sct ON sct.id = sa.taxID";
$dbconfig = include __DIR__ . "/config.php";
$pdo = new PDO('mysql:host=127.0.0.1;dbname='.$dbconfig["db"]["dbname"], $dbconfig["db"]["username"], $dbconfig["db"]["password"]);
$connection = $pdo->prepare($query);
$connection->execute();
$result = $connection->fetchAll(PDO::FETCH_ASSOC);
PS: ausserdem steht SUM() nicht in jeder DB einfach so zur Verfügung
$query = "SELECT
sad.ordernumber AS ARTIKELNR,
sad.articleID AS ARTIKELID,
sad.instock AS Warenbestand,
sad.ean AS ean,
ROUND((sap.price*sct.tax)/100+sap.price,2) AS VK
FROM s_articles_details AS sad
INNER JOIN s_articles AS sa ON sa.id = sad.articleID
INNER JOIN s_articles_prices AS sap ON sap.articleID = sa.id
INNER JOIN s_core_tax AS sct ON sct.id = sa.taxID";
$connection = Shopware()->Container()->get('dbal_connection');
$data = $connection->fetchAll($query);
Ich habe jetzt nicht das Query ausprobiert, aber ICH würde mind. bei der Tabelle „s_articles_prices“ nicht nach „articleID“ sondern nach „articledetailsID“ gehen. Die jetzige Abfrage könnte bei Varianten-Artikeln Probleme verursachen.