开发者

how to construct following query (left-right join and multiple update) in zend framework?

table1(id, field1, field2, field3), table2(id, table1_id, field1, field2, field3), and the query goes like this

开发者_开发百科
select table1.*, table2.field2 as newfield from tabel1 
left join on table2 on table1.id = table2.table1_id;

I tried following code on Model

$select  = $this->select('table1.*', 'table2.field1')
->from('table1')
->joinLeft('table1', '`table2`.`table1.id` = `table1.id`.`table2.table1_id`')
->setIntegrityCheck(false);

but got result that i would get from inner join. Please Confirm me.

and I also would like to know how to construct following query in Model

update table1 set field1 = 'somevalue' where id > someinteger.

regards


For the select query:

// select(false) because you need to declare an identifier for table 1
// 't1' => $this->_name is better if this function is in the DbTable model
// setIntegrityCheck(false) because you need to join tables - you did this correctly
// joinLeft() arguments: array(table2id => table2_name),
//   'table2id.field = table1id.field', array(table2id.field as newfieldname)

$select = $this->select(false)
               ->from(array('t1' => 'table1'))
               ->setIntegrityCheck(false)
               ->joinLeft(
                   array('t2' => 'table2'),
                   't2.table1_id = t1.id',
                   array('t2.field2 as newfield')
               );

For the update query (within the DbTable model):

$data = array('field1' => 'somevalue');
$where = 'id > someinteger';
$this->update($data, $where);

EDIT: Update and Increment Field in Multiple Rows

In SQL, you would do something such as the following:

UPDATE table_name SET field_name = field_name + x WHERE field_name < 5

Thus, using the code I listed above for update, you would do the following in ZF:

$this->update( array('field_name' => 'field_name + x') , 'field_name < 5' );

where of course x is the value by which you wish to increment everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜