Zend Update Query - php

How to write below mysql query using zend syntax?
UPDATE core_user_transaction as t
JOIN core_user_transaction_therapy AS cuts ON cuts.tid=t.tid
JOIN therapy_booking AS tb ON cuts.conf_id = tb.conf_id
JOIN therapy_service_fees AS fees ON cuts.fee_id=fees.id
JOIN therapy_service_taxes AS tst ON fees.service_id=tst.service_id
JOIN core_tax AS ct ON ct.tax_id = tst.tax_id
SET t.tax=ROUND(t.fee*(ct.tax_value/100),2),
t.amount = (ROUND(t.fee*(ct.tax_value/100),2)+t.fee)
WHERE tb.datetime >= '2015-10-01 00:00:00'
AND t.state = 'PENDING'
AND t.status ='ACTIVE'

Try this:
$adapter = new Zend\Db\Adapter\Adapter($driverConfig);
$sql = "YOUR_QUERY";
$statement = $adapter->query($sql);
$statement->execute();
Also please refer Zend\Db\Adapter

There is no possible with the 'update()' method provided by the Zend Db Adapter.
You can however, run the query manually using the adapter. Something like:
// $adapter is an instance of Zend_Db_Adapter
$adapter->query(YOUR QUERY HERE);

Related

MySQLi join using date(now()) in where clause

I am running this query in phpmyadmin and it works fine. But on running it in joshcam's MySQLi Database class it gets wrong data
Query:
SELECT
s.az
, s.ta
, s.zamanSarfShode
, p.name
FROM
saateruzane s
JOIN
projhe p
JOIN
kareruzane k
WHERE
s.ProjheId = p.id
AND
k.id = s.ruzId
AND
k.ruzGregorian = date(now())
PHP code :
$con->join('projhe p', 's.ProjheId = p.id');
$con->join('kareruzane k', 'k.id = s.ruzId');
$con->joinWhere('kareruzane k','k.ruzGregorian', 'date(now())');
$tines = $con->get('saateruzane s',null,'s.az ,s.ta ,s.zamanSarfShode ,p.name');
The definition for joinWhere is
public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
So you don't need to pass in every part. Instead, try this:
$con->joinWhere('kareruzane k','k.ruzGregorian = date(now())');
Though better than date(now()) is CURDATE()
$con->joinWhere('kareruzane k','k.ruzGregorian = curdate()');
However, I'm not sure you should be using a join here, since it's part of the WHERE clause on your original query, not the JOIN clause. So instead do
$con->where('k.ruzGregorian = curdate()');
With just a fast look on this lib docs, I found this:
'createdAt' => $db->now(),
// createdAt = NOW()
https://github.com/joshcam/PHP-MySQLi-Database-Class#insert-query
In your case try use
$con->joinWhere('kareruzane k','k.ruzGregorian', $con->now());

How would I write this query that includes joins in CodeIgniter's query builder?

The query I'm trying to write is
SELECT Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate
FROM Equipment, LoanedOut
WHERE Equipment.EquipmentRecordID = LoanedOut.EquipmentRecordID AND LoanedOut.StudentNumber = 040828055
I can't figure out how to do it using the query builder for codeigniter, the best I have so far is
$this->db->select('Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate');
$this->db->from('Equipment e, LoanedOut l');
$this->db->join('l', 'e.EquipmentRecordID = l.EquipmentRecordID')->join('l', 'l.StudentNumber', $studentNumber);
This will give you what you want//
$sql = 'SELECT e.Name,e.EquipmentTag,l.StudentNumber,l.DueDate FROM Equipment e LEFT JOIN LoanedOut l ON e.EquipmentRecordID = l.EquipmentRecordID WHERE l.StudentNumber = "040828055"'
$query = $this->db->query($sql);

Zend Select particular set of columns from multiple tables

Given a query :
SELECT t1.c1, t2.c1 FROM t1,t2;
How to write it in Zend framework without using joins ?
Try the below join query in zend framework,
$sql = $this->tableGateway->getSql()->select();
$sql->join('table2', "table1.column1 = table2.column1", array(column1,column2), 'inner');
Assuming you have a common field (we'll call it c2), you can do something approximating a JOIN without actually doing a JOIN.
// we'll assume $adapter is an instance of Zend_Db_Adapter_Mysqli
$cols = $adapter->fetchAll(
'
SELECT
t1.c1 AS t1c1,
t2.c1 AS t2c1
FROM t1, t2
WHERE t1.c2 = t2.c2
'
);
Using Zend_Db_Select you can do it this way
$rows = $adapter->select()
->from('t1', array('t1c1' => 'c1'))
->from('t2', array('t2c1' => 'c1'))
->where('t1.c2 = t2.c2')
->query()->fetchAll();

PHP Zend Joininner

I have a SQL statement
select a.username
from friendships f1
inner join friendships f2 on (f1.user = f2.friend and f1.friend = f2.user)
inner join users a ON (a.user_id = f1.friend) where f1.user = '24'
I excuted this query on phpmyadmin and it's OK.
But I don't knoe how to do this query in Zend
$select = $this->_db_table->select()
->setIntegrityCheck(false)
->from(array('f1' => 'friendships'), array('u.*'))
->joinInner(array('f2' => 'friendships'),
array('f1.user = f2.friend', 'f1.friend = f2.user'))
->joinInner(array('u' => 'users'), array('u.user_id = f1.friend'))
->where('f1.user = ?', $user_id);
I use this code but it doesn't work, any ideas?
What version of ZF are you using ? Here is how you would do it in ZF 1.12:
$this->_db_table->getAdapter()->select()
->from("friendships as f1")
->joinInner("friendships as f2", "f1.user = f2.friend AND f1.friend = f2.user")
->joinInner("users as a", "a.user_id = f1.friend", array("a.username"))
->where("f1.user = ?", $user_id)
->query()
->fetchAll(); // use ->fetch() if you only want to retrieve one record
Read more on in the manual:
What exactly doesn't work? Are you getting a PHP/Zend error or a SQL error?
You can use $select->assemble() to examine the prepared query and compare it against your expectation:
echo $select->assemble();

Zend_Db_Select: Working with JOIN's

I have this query:
SELECT
groups.name
categories.name,
categories.label
FROM
groups
JOIN
categories
ON
(categories.group1 = groups.id
OR
categories.group2 = groups.id)
AND
groups.label = :section
AND
categories.active = 1
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$select->from($dao, array('groups.name', 'categories.name', 'categories.label'))
->join(array('categories', 'categories.group1 = groups.id OR categories.group2 = groups.id'))
->where('groups.label = ?', $group)
->where('categories.active = 1');
My error:
Exception information:
Message: Select query cannot join with
another table
Does anyone know what I did wrong?
UPDATE / SOLUTION:
I've found the solution thanx to Eran. I just post the solution here in case anyone else is stuck on a problem like this one. The solution is:
$db = Zend_Registry::get('db');
$dao = new Default_Model_Db_CategoryDao('db');
$select = $dao->select();
$select->setIntegrityCheck(false)
->from(array('c' => 'categories'), array('name', 'label'))
->join(array('g' => 'groups'), 'c.group1 = g.id OR c.group2 = g.id', 'g.label')
->where('g.label = ?', $group)
->where('c.active = 1');
return $dao->fetchAll($select);
You are using a Zend_Db_Table_Select object. Those by default have integrity check enabled and cannot select data that is outside of their table.
You can turn it off by adding -> setIntegrityCheck(false) to the select object before querying with it.
You can read more about it in the manual under Select API -> Advanced usage

Categories