0

I am trying to insert multiple data with single query using zend 2. For reference below is the code . It is not throwing any error but data is not getting inserted. I am calling "mapOffers" from controller where I am passing the indexed array "$assigned_b"..Kindly help me with this.

namespace Offers\Model;
   use Zend\Db\Adapter\Adapter;
   use Zend\Db\TableGateway\AbstractTableGateway;
   use Zend\Db\Sql\Sql;
   use Zend\Db\Sql\Select;
   use Zend\Db\Sql\Where;
use Zend\Db\Sql\Insert;
use Utility\Utility;
class OffersTable extends AbstractTableGateway {

    protected $table = 'offers';
    protected $joinTable = 'organization';
    protected $b_offer = 'b_offers';
    protected $sql_object,$select_object;


    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
    }


    public function mapOffers($assigned_b,$offer_id){

        $sql = new Sql( $this->adapter );
        $delete = $sql->delete($this->beacon_offer)->where("offer_id = $offer_id");
        $deleteString = $sql->getSqlStringForSqlObject($delete);

        $query = 'INSERT INTO '.$this->offers . ' (`offer_id`, `b_id`) VALUES ';
        $queryVals = array();

         foreach ($assigned_b as $_bid) {
              if($_bid){
                $queryVals[] = "(".$offer_id.",".$_bid.")";  
              }
         }

         $stmt = $this->adapter->query($query . implode(',', $queryVals));

    }




}
ssnegi
  • 183
  • 1
  • 1
  • 15
  • The above looks okay, so something is wrong in other place. You may want to turn logging on for general query log for your database to see exactly what SQL is being passed to it. Or, you can also print out the query directly in the code, like `print $query . implode(',', $queryVals);` to see the entire SQL and paste that SQL yourself into the command line of your database client. That can show you any errors you may be missing. – Dennis Jul 14 '17 at 13:19
  • $this->offers looks wrong, can you replace it with $this->table and see? – Jannes Botis Jul 14 '17 at 15:03
  • Hello Dennis, I already print the query and it seems correct . Because when i run that query in the mysql db it inserted the record . Could you tell me some other way so I may run the query. – ssnegi Jul 15 '17 at 05:35

1 Answers1

0

you need to call the execute function. It has to look like that (example from my own project)

$this->adapter->query($query)->execute();
tookie009
  • 186
  • 1
  • 14