Magento How to connect to a database:
1
Simple way. Restrictive small
1.1
$write = Mage::getsingleton (' Core/resource ')->getconnection (' Core_write ');
$readresult = $write->query ("Select eav_attribute_option_value.option_id, Eav_attribute_option_value.value from Eav_attribute_option_value
INNER JOIN eav_attribute_option on eav_attribute_option_value.option_id=eav_attribute_option.option_id WHERE
eav_attribute_option.attribute_id=505 and eav_attribute_option_value.option_id in (". $brands."));
while ($row = $readresult->fetch ()) {
$brandlist [] = Array (' value ' => $row [' option_id '], ' label ' => $row [' value ']);
}
1.2
$resource = Mage::getsingleton (' Core/resource ');
$read = $resource->getconnection (' Catalog_read ');
$select = $read->select ()
Get Zend_db_select
->from (' CP ' => $categoryProductTable))
->join (' Pei ' => $productEntityIntTable), ' pei.entity_id=cp.product_id ', Array ())
->joinnatural (' ea ' => $eavAttributeTable))
->where (' cp.category_id=? ', $categoryId)
->where (' pei.value=1 ')
->where (' ea.attribute_code= "Terry");
$rows = $read->fetchall ($select);
$ids = Array ();
foreach ($rows as $row) {
$ids [] = $row [' product_id '];
}
$ret = Implode (', ', $ids);
return $ids;
2
Generally used for Magento, products, classification and other information modification.
Pass
It is generally said that the collection in the EAV model
Mage_catalog_model_resource_eav_mysql4_product_collection
As long as the class that implements the following class,
Varien_data_collection
can be used
Public Function Getselect ()
{
$this->_select = $this->_conn->select ();
return $this->_select;
}
Get Varien_db_select.
3
Queries and modifications for a single product
Based on the ID value. or unique value.
$model = Mage::getmodel (' Blog/blog ')->load ($id);
The information in this way is more comprehensive.
4
$coll = Mage::getmodel (' Blog/tag ')->getcollection ();
$sel = $coll->getselect ();
$coll->getselect ()
5
For Mage::getresourcemodel ()
and Mage::getmodel ()->getcollection ();
Get collection instance.
_setresourcemodel ($resourceName, $resourceCollectionName =null)
So
5.1 Mage::getresourcemodel ()
and Mage::getmodel ()->getcollection ()
and EAV model, all can use Getselect ();
6
Mage_core_model_mysql4_abstract
1
Non-EAV model:
1.1
Resource Collection Model:
Aw_blog_model_mysql4_blog_collection extends Mage_core_model_mysql4_collection_abstract
Mage_core_model_mysql4_collection_abstract extends varien_data_collection_db
VARIEN_DATA_COLLECTION_DB extends Varien_data_collection
1.2
Resource model
Aw_blog_model_mysql4_blog extends Mage_core_model_mysql4_abstract
Mage_core_model_mysql4_abstract extends Mage_core_model_resource_abstract
2
For the EAV model:
2.1
Resource Collection Model:
Mage_catalog_model_resource_eav_mysql4_product_collectionextends mage_catalog_model_resource_eav_mysql4_ Collection_abstract
Mage_catalog_model_resource_eav_mysql4_collection_abstract extends Mage_eav_model_entity_collection_abstract
Mage_eav_model_entity_collection_abstract extends varien_data_collection_db
VARIEN_DATA_COLLECTION_DB extends Varien_data_collection
2.2
Resource model
Class Mage_catalog_model_resource_eav_mysql4_abstract extends Mage_eav_model_entity_abstract
Mage_eav_model_entity_abstract
Extends Mage_core_model_resource_abstract
Implements Mage_eav_model_entity_interface
Model--->
_getresource (get resource model)
In resource, the methods you can use are:
_getreadadapter ()
_getwriteadapter ()----> can then be varien_db_select by means of the public function select ();