Symfony Doctrine_Query DELETE with INNER JOIN - doctrine

I am using symfony+doctrine, and I want to perform a delete query with a join. See below for my code I am currently using that works fine.
$sql = 'DELETE a
FROM a
INNER JOIN b ON a.b_id = b.id
WHERE b.c_id = :c_id';
$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$stmt = $pdo->prepare($sql);
$params = array('c_id' => $c_id);
$stmt->execute($params);
Anyone know how I can do this using:
Doctrine_Core::getTable('a')
Or
Doctrine_Query::create()->delete()->from('a')
I have had no luck with either.
I just don't really want to use raw SQL in my app.

Something like this should do it
Doctrine_Query::create()
->delete('a a')
->innerJoin('a.b b')
->where('b.c_id = ?', $c_id)
->execute()

Related

How create it in Laravel Query builder

SELECT e.match_id, e.team_id
FROM matches
LEFT JOIN match_events e
ON e.match_id = matches.id AND e.match_event_type_id = 1
Try this,
$result = DB::table('matches as M')
->leftjoin('match_events as E','E.match_id','=','M.id')
->where('E.match_event_type_id',1)
->select(['E.match_id','E.team_id'])
->get();

Can not get the url parameter in PHP

I am trying to get URL parameter in SQL, but nothing happens.
Here is my URL:
http://localhost/webshop/imagegallery.php?categori=necklace
Here is my SQL query:
$sql = 'SELECT count(productid) FROM products where productcategori=".$_GET["categori"]"';
What am I doing wrong?
Have a look at this query, too:
$sql = 'select * from products join ids on products.productid=ids.productid join photos on photos.photosid=ids.photoid where products.productcategori='".$_GET["kategori"]."' && ids.photonumber=1 ORDER BY products.productid DESC $limit';
First of all, your quotation marks seem to be the problem. Try changing your query line to this:
$sql = "SELECT count(productid) FROM products where productcategori='".$_GET["categori"]."'";
Further, you should never insert variables into a SQL query like this. Never.
The reason is that like this, your system is vulnerable for SQL injections.
Instead consider using PDO. This SO question has a nice answer on how to do it correctly.
Using that answer, this is some example code regarding the last part of your question. Note that I replaced all variables in your query string by PDO placeholders.
<?php
$pdo = new PDO('mysql:dbname=mydatabase;host=127.0.0.1;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM products JOIN ids ON products.productid=ids.productid JOIN photos ON photos.photosid=ids.photoid WHERE products.productcategori=:categori && ids.photonumber=1 ORDER BY products.productid DESC LIMIT :limit_min , :limit_max";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':categori', $_GET['categori']);
$stmt->bindParam(':limit_min', ($pagenum - 1) * $page_rows, PDO::PARAM_INT);
$stmt->bindParam(':limit_max', $page_rows, PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row) {
// do something with $row
}
?>

Where by month in codeigniter

I have this code
$where = array('MONTH(c.cashDate)' => 'MONTH(NOW())');
$data['dataCash'] = $this->cash->cashList($where);
but it doesn't work, its create query like this
SELECT `c`.`cashId`, `c`.`cashDescription`, `c`.`cashDate`, `c`.`cashCategory`, `a`.`category`, `c`.`cashValue`, `t`.`typeid`, `t`.`type` FROM (`cash` c) LEFT JOIN `cashCategory` a ON `c`.`cashCategory` = `a`.`categoryId` LEFT JOIN `type` t ON `a`.`type` = `t`.`typeid` WHERE MONTH(c.cashDatee) = 'MONTH(NOW())' ORDER BY `c`.`cashDate` desc
when I try with phpmyadmin, it doesnt have a row
but when I remove ' from MONTH(NOW()) like this
SELECT `c`.`cashId`, `c`.`cashDescription`, `c`.`cashDate`, `c`.`cashCategory`, `a`.`category`, `c`.`cashValue`, `t`.`typeid`, `t`.`type` FROM (`cash` c) LEFT JOIN `cashCategory` a ON `c`.`cashCategory` = `a`.`categoryId` LEFT JOIN `type` t ON `a`.`type` = `t`.`typeid` WHERE MONTH(c.cashDatee) = MONTH(NOW()) ORDER BY `c`.`cashDate` desc
it works, how I call func in controller?
when you do this, it wont work
$where = array('MONTH(c.cashDate)' => MONTH(NOW()));
Just update your $where condition
$where = array('MONTH(c.cashDate)' => 'MONTH(NOW())');
into
$where = 'MONTH(c.cashDate) = MONTH(NOW())';

Multiple condition in Join Magento

Can Anyone tell me?
How can i write this type of query in magento
(Multiple condition in on)
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON (Customers.CustomerID=Orders.CustomerID and Orders.status = 1)
//I know this type
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'));
How can i add multiple condition in JOIN ?
I found the answer myself, It turnout to be very easy
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key and table_alias.columnname = ".."' , array('table_alias.*'));

codeigniter join not working

<?php
$this->db->select('*');
$this->db->from('venue');
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
$this->db->join('venue_type vt2', 'vt2.venue_type_id = venue.venue_type_id2');
$this->db->join('venue_subtype vst2', 'vst2.venue_subtype_id = venue.venue_subtype_id2');
$this->db->join('venue_type vt3', 'vt3.venue_type_id = venue.venue_type_id3');
$this->db->join('venue_subtype vst3', 'vst3.venue_subtype_id = venue.venue_subtype_id3');
$this->db->where('venue_id',$id);
$query = $this->db->get();
i have venue table it has more then 1 field relation b/w venue_type. When i try to give first relation
<?php
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
its working fine , but i try to access whole its not working.
Please Help me. (It may simple but i stuck)
By Saravanan.
You need to use alias for multiple joins.
SELECT st_id, table1.us_login, table2.us_login
FROM (stream)
LEFT JOIN users AS table1 ON table1.us_id = stream.st_id_user_from
LEFT JOIN users AS table2 ON table2.us_id = stream.st_id_user_to
see the link: http://codeigniter.com/forums/viewthread/151176/
There's no $this->db->from function in Codeigniter. Use instead $this->db->select('venue.*')

Resources