How to join tables in Zend Framework? - php

Hi I have my query in codeigniter since I I have switch to Zend Framework, I wanted do my query in zend framework:
Here is my sample query:
$sql = "SELECT Mes.fromid, Mes.is_read AS is_read, Mes.id AS mesid, Mes.message AS message,
max(Mes.date) AS date, User.username AS username, User.id AS Uid
FROM `messages` AS Mes
LEFT JOIN users AS User ON User.id = Mes.fromid
WHERE Mes.toid = ? AND Mes.fromid <> ?
GROUP BY Mes.fromid ORDER BY date DESC";
$query = $this->db->query($sql, array($fromid, $fromid));
return $query->result();
Thank you for you help.

$sql = $db ->select()
->from('messages as mes',array('fromid','is_read','id as mesid','message','max(date) as date'))
->join('users as user','user.id = mes.fromid',array('username','id as uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');

$select = $this->select()
->setIntegrityCheck(false)
->from(
array('Mes' => 'messages'),
array('fromid', 'is_read AS is_read', 'id AS mesid', 'message AS message',
'max(Mes.date) AS date')
)
->joinLeft(array('User' => 'users'), "User.id = Mes.fromid", array('username AS username', 'id AS Uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');
Hope This helps :)

Check out Zend_Db_Statement, it looks like you'll have to make very few edits to make this work.
Probably just change out the Db adapter to a Zend adapter and it'll probably work.

Related

OR WHERE in zend framework

How can I write query like this In Zend Framework and fetch all the rows
SELECT * FROM tbl
WHERE user_id = $part_mail
OR user_id ='$id3';
I used to try this :
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
-> orwhere('user_id = ?', $id3 ));
Following ways should help you.
Solution 1:
where( "user_id = '$part_mail' OR user_id = '$id3'" );
Solution 2:
$list = array( $part_mail, $id3 );
...
where( 'user_id in ( ? )', $list );
Solution 3:
$list = array( $part_mail, $id3 );
...
where( array( 'user_id' => $list ) );
Refer to Documentation:
Example #17 Example of an array parameter in the where() method
-You can pass an array as the second parameter to the where() method when using the SQL IN operator.
orwhere available in Zend Framework 1. But there is a space in your code before orwhere(). Check the code below:
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
->orwhere('user_id = ?', $id3 ));

writing a subquery using Zend_Db_Table_Select

I am not able to convert the below query into Zend_Db_Table_Select query.
SELECT GROUP_CONCAT(wallet_transaction_id) as wallet_transaction_ids,transaction_type,source,status
FROM(
SELECT wallet_transaction_id, transaction_type, source, status
FROM ubiw_transactions_wallet
WHERE (game_id = '1292') AND (player_id = 1538)
ORDER BY date DESC LIMIT 100
) a
GROUP BY a.transaction_type,a.status, a.transaction_type;
any help is appreciated.
thanks,
shiv
// Maybe need some changes
$table = new Zend_Db_Table('ubiw_transactions_wallet');
$subSelect = $table->select()
->from('ubiw_transactions_wallet', array('wallet_transaction_id', 'transaction_type', 'source', 'status'))
->where($table->getAdapter()->quoteInto('game_id = ?', 1292))
->where($table->getAdapter()->quoteInto('player_id = ?', 1538))
->order('`date` DESC')
->limit(100);
$mainSelect = $table->select()
->setIntegrityCheck(false)
->from(array('a' => $subSelect), array('wallet_transaction_ids' => new Zend_Db_Expr('GROUP_CONCAT(wallet_transaction_id)'), 'transaction_type', 'source', 'status'))
->group(array('a.transaction_type', 'a.status', 'a.transaction_type'));
$result = $table->fetchAll($select);

try to perform a query with join with Zend (SE)

I have 4 tables on my DB;
merchants, pay_method, spendings ans transaction_type.
They all have an id and a title, except spend (spend have the Foreign keys of the others and other field like user_id, comment, ... ).
I try to have all the informations of all the tables for an unique user_id!
for perform it, I tried a little test that doesn't run :
public function getSpendingsForUser( $userid )
{
$mercTable = Engine_Api::_()->getDbTable('merchants', 'spendings');
$mercName = $mercTable->info('name');
$table = Engine_Api::_()->getDbTable('spendings', 'spendings');
$spendName = $table->info('name');
$select = $table->fetchAll(
$table->select()
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
return $select;
}
An in the view file :
foreach($this->spendings as $s) {
var_dump($s->comment);
}
How can I make the join correctly to have all the info from the user_id ???
Thanks a lot !!!
You should define doesn't run to help us help you. But I can tell you you probably need to ->setIntegrityCheck(false);
$select = $table->fetchAll(
$table->select()
->setIntegrityCheck(false);
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
Before the join.

Zend framework leftJoin

Here is the code that doesn't work.
$select = $tariffsTable->select(Zend_Db_Table_Abstract::SELECT_WITHOUT_FROM_PART)
->from('tariff', null)
->where('id = ?', $this->id)
->joinLeft('characteristic_value',
'characteristic_value.tariff_id = id',
array('value_' . $locale, 'characteristic_id'))
->joinLeft('characteristic',
'id = characteristic_value.characteristic_id',
array('name_' . $locale, 'alias'));
$select->setIntegrityCheck(false);
$tariffCharacteristics = $tariffsTable->fetchAll($select)->toArray();
Thank you for help! I have solved the problem. Here is working code:
$select = $tariffsTable->select(Zend_Db_Table_Abstract::SELECT_WITHOUT_FROM_PART)
->from('tariff', null)
->joinLeft( array('characteristic_value'),
'characteristic_value.tariff_id = tariff.id',
array('value_' . $locale))
->joinLeft(array('characteristic'),
'characteristic.id = characteristic_value.characteristic_id',
array('name_' . $locale, 'alias'))
->where('tariff.id = ?', $this->id);
$select->setIntegrityCheck(false);
It is because your are using id column on WHERE and/or LEFT JOIN ON clause while an id column should be present in both table your are trying to join.
You need to specify which id should be used in both case.
Like characteristic.id or characteristic_value.id or tarriff.id.
You can use alias by using array as a parameter for from() and joinLeft() methods.
$select->from(array('t' => 'tarriff'))
->where('t.id = ?', $this->id);

Doctrine Join DQL

I's like to do a join between 2 tables on a specific ID. At the moment, I have this DQL:
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_Item i ON e.itemId = i.itemId')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
The result is empty, although my eventId has a value ... Can you help me please? I there somewhere a tutorial on DQL-joins? I don't get it right with the help of the Doctrine documentation.
Thanks!
PS I have doctrine working in combination with Zend Framework.
you need add a relation to the model and join the tables using the relation
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
you should change the name in the left join from Model_EventItem to e
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e, e.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');

Categories