Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
//There is a more proper way to do this.
$resource = Mage::getSingleton('core/resource');
$write = $resource->getConnection('core_write');
$table = $resource->getTableName('your/model');
//You can Create:
$write->insert(
$table,
array('column_1' => 1, 'column_2' => 2)
);
//Read:
$select = $write->select()
->from(array('tbl' => $table), array('entity_id', 'company'))
->join(array('tbl2' => $table2), 'tbl.entity_id = tbl2.product_id', array('stuff'))
->where('name LIKE ?', "%{$name}%")
->group('company');
$results = $write->fetchAll($select);
//Update:
$write->update(
$table,
array('column_1' => 3, 'column_2' => 4),
array('entity_id = ?' => 123)
);
//Delete:
$write->delete(
$table,
array('entity_id IN (?)' => array(123, 456))
);
//Insert Multiple:
$rows = array(
array('col_1'=>'value1', 'col_2'=>'value2', 'col_3'=>'value3'),
array('col_1'=>'value3', 'col_2'=>'value4', 'col_3'=>'value5'),
);
$write->insertMultiple($table, $rows);
Insert Update On Duplicate:
$data = array();
$data[] = array(
'sku' => $sku,
'name' => $name
);
$write->insertOnDuplicate(
$table,
$data,
array('name') // this is the fields that will be updated in case of duplication
);
//Escape in direct querys
$query = "insert into mage_example "
. "(name, email, company, description, status, date) values "
. "(:name, :email, :company, :desc, 0, NOW())";
$binds = array(
'name' => "name' or 1=1",
'email' => "email",
'company' => "company",
'desc' => "desc",
);
$write->query($query, $binds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.