简体   繁体   中英

Update with WHERE and JOIN in ZF2

I am trying to Update a value in MySQL database in Zend Framework 2. I want to use where and Join in the table. Table structure is

-----Credit-----
   ContactID
     Credits

-----Token------
   ContactID
    Token

I want to write the following MYSQL query

"Update credit_details 
     LEFT JOIN token ON token.CONTACT_ID = credit.CONTACT_ID 
 SET CREDITS = '200' 
 WHERE TOKEN ='$token';".

So far I have the following code but it seems to not work.

$this->tableGateway->update(function (Update $update) use ($token){
        $update->set(array('CREDITS'=>$credits))
        ->join('token','token.CONTACT_ID=credit.CONTACT_ID', array( 'CONTACT_ID'=>'CONTACT_ID'
        ),'left')
        ->where($this->tableGateway->getAdapter()->getPlatform()->quoteIdentifierChain(array('token_details','TOKEN')) . ' = ' . $this->tableGateway->getAdapter()->getPlatform()->quoteValue($token));
    });

To give some clarification. There isn't an abstraction layer for UPDATE with JOIN. The Zend Update DBAL doesn't have a join method to call.

Reference: Zend\\Db\\Sql\\Update

In order to perform an update with join using ZF2's table gateway you would need to extend the table gateway and write your own updateJoin method or extend the Sql\\Update object as say UpdateJoin and add a join method.

In order to use the tableGateway to update with join without extending the ZF2 objects you would need to do something like below

Reference: ZF2 Documentation

<?php
/* Define a token for the example */
$token = 12345;

/* create a new statement object with the current adapter */
$statement = $this->tableGateway->getAdapter()
    ->createStatement(
        'UPDATE credit_details
        LEFT JOIN token
        ON token.CONTACT_ID = credit_details.CONTACT_ID
        SET credit_details.CREDITS = 100
        WHERE token.TOKEN = ?' 
    );

/* create a new resultset object to retrieve results */
$resultSet = new Zend\Db\ResultSet\ResultSet;

/* execute our statement with the token and load our resultset */
$resultSet->initialize( $statement->execute( array( $token ) ) );

/* display the affected rows */
echo $resultSet->count();

Off Topic

Also to offer a bit of advice that may save you some hassle in the future. When using the ZF2 DBAL and you have an adapter and driver specified, the driver will handle quote identifiers and values for you. Unless you specifically use Zend\\Db\\Sql\\Expression or Zend\\Db\\Sql\\Literal where you would need to handle quote identifiers and values. In most cases you can use Zend\\Db\\Sql\\Predicate\\Predicate in the where call, which is my prefered method.

Reference: Zend\\Db\\Sql Documentation

EG:

<?php
$adapter = new Zend\Db\Adapter\Adapter($configArray);
$sql = new Zend\Db\Sql\Sql($adapter);
$update = $sql->update( 'credit_details');
$update->set( array('CREDITS' => $credits) );
$update->where( array( 'CONTACT_ID' => $contact_id ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM