[TYPO3-english] run a simple query to check for existence of a value
Calgacus map Brude
calgach at gmail.com
Wed Nov 13 18:58:50 CET 2013
Hi,
Is there a built in simple function or standard way of running a a check to see if a certain value exists in the database?
I want to check to see if a given barcode has already been used - it would exist in my coupons table in the barcode column.
I could just read up all coupons from the repo and loop over them looking but that would be slow for something like this. If not then how can I write my own sql and use the createQuery function with typo3 v4.5.3 and extbase? are there some code snippets up that show how to work with that in my version? eg http://forum.typo3.org/index.php?t=msg&goto=534574&
I tried like this, I put this method in my repo class:
/**
* @param string $code
* @return Tx_Extbase_Persistence_QueryInterface
*/
public function findBarcode($code) {
echo "<br/>".__FUNCTION__.__LINE__."<br/>";
$query = $this->createQuery();
echo "<br/>".__FUNCTION__.__LINE__."<br/>";
$query->statement("SELECT count( barcode) FROM tx_bpscoupons_domain_model_coupon where barcode like '$code'");
echo "<br/>".__FUNCTION__.__LINE__."<br/>";
return $query->execute();
}
then this method in my controller
* check to see if there is already a coupon with this barcode in the database
* @param int $barcode
* @return bool
*/
public function isBarcodeUnique($barcode){
echo "<br/>".__FUNCTION__.__LINE__."<br/>";
$rec = $this->couponRepository->findBarcode($barcode);
echo "<br/>".__FUNCTION__.__LINE__."<br/>";
var_dump($rec);
return true; //for testing, until I put ion the logic
}
the code runsand hits the line $rec = $this->couponRepository->findBarcode($barcode); and crashes there - I get a blank page as output and the error logs show nothing. if I comment out that line the code does not crash and everything works as expected.
More information about the TYPO3-english
mailing list