Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Page 1 of 2
How to write a Custom SQL Query? 
 
DevHasMag
Jr. Member
 
Total Posts:  18
Joined:  2007-12-19
 

Hello All,

I request anyone to please give me a solution to write my own SQL query.
I have a controller like this.
class Mage_Sampleorder_TsampleController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
I want to execute my own query in the function , something like $query->setquery(’insert into tablename values (’aaa’,bbb’’,ccc’’) ‘);
Can anyone please give me a solution with steps?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

I’m not sure what exactly you are trying to do, but here’s a way to run SQL query in Magento:

<?php

// fetch write database connection that is used in Mage_Core module
$write Mage::getSingleton('core/resource')->getConnection('core_write');

// now $write is an instance of Zend_Db_Adapter_Abstract
$write->query("insert into tablename values ('aaa','bbb','ccc')");

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
DevHasMag
Jr. Member
 
Total Posts:  18
Joined:  2007-12-19
 

Hello Moshe,
What i am doing is this.
I have a form in sampleorder/tsample with 5 steps.
1) address 2) shipping 3) information form 4) payment 5) review
First time i open http://mydomain.com/sampleorder/tsample/ i get address form.
i will enter the details of address and submit and go to next step.
my address details should get saved in a different table which i have created.
So i want to write my own query in my tsample controller.
Can you tell me how to write?
Also can you tell me how to check session and get the address of logged in customer id?
Please provide me a solution. I need help as i am new to magento.

Regards
DevHasMag,

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

That sounds to me like whole checkout process.

Are you going to create the whole database structure and html forms for this process?
Or you were looking to use Magento’s models and database structure?

In first case i do not see why it should be part of Magento, in 2nd case I do not think that I will be able to address all specifics and details of custom checkout process.

If you are trying to do this only for learning interest, here’s how you would get customer info:

/** var $session Mage_Customer_Model_Session */
$session Mage::getSingleton('customer/session');

/** var $customer Mage_Customer_Model_Customer */
$customer $session->getCustomer();

/** var $address Mage_Customer_Model_Address */
$address $customer->getPrimaryBillingAddress();

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
DevHasMag
Jr. Member
 
Total Posts:  18
Joined:  2007-12-19
 

Hi Moshe,
Thank you so much. The requirement is i need to create new form with 5 different steps. The form is similar to checkout but the fields are totally different except for the address.
There are 2 things,
1) checkout as a guest - the details will be a new entry to customers and address table
2) login - if logged in, it has to fetch details of the logged in customer from the same table.
Can you please tell me how to go about this.
Also i would like to know the tables related to fetch customers details right from name,pwd,etc…
Can you explain please?

Regards,
DevHasMag.

 
Magento Community Magento Community
Magento Community
Magento Community
 
YoavKutner
Magento Team
 
Avatar
Total Posts:  491
Joined:  2007-08-08
 

DevHasMag - as we are focusing our efforts here at Varien on getting the first stable version of Magento released, please wait before trying to implement such massive customizations until we release some documentation.

Thank you for your patience

yoav

 
Magento Community Magento Community
Magento Community
Magento Community
 
tronics
Member
 
Total Posts:  46
Joined:  2008-01-17
 

Here you can see a full example, although this might not make sense.. but this is how to custom query..

$write Mage::getSingleton('core/resource')->getConnection('core_write');

// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT *
FROM `pepitashop_catalog_product_super_attribute_pricing` , `pepitashop_catalog_product_entity`,`pepitashop_catalog_product_super_attribute`
WHERE
`pepitashop_catalog_product_super_attribute`.product_super_attribute_id = `pepitashop_catalog_product_super_attribute_pricing`.product_super_attribute_id
AND
pepitashop_catalog_product_super_attribute.product_id = `pepitashop_catalog_product_entity`.entity_id
AND pepitashop_catalog_product_super_attribute.product_id = '"
.$_id."'
ORDER BY `pepitashop_catalog_product_super_attribute_pricing`.pricing_value DESC
LIMIT 0 , 30 "
);


while (
$row $readresult->fetch() ) {
$categoryIds[]
=$row['id'];
}

foreach ($categoryIds as $entity_id{
$write
->query'INSERT INTO cataloginventory_stock_item
(`product_id`,`stock_id`,`qty`,`is_in_stock`)
VALUES ( '
.$_id.', 1, 99999, 1)' );
}

Regards,
tronics

 
Magento Community Magento Community
Magento Community
Magento Community
 
chavansoft
Jr. Member
 
Total Posts:  1
Joined:  2008-11-12
 

Hi Tronics

Thank you for your full example.

Regards
chavan

 
Magento Community Magento Community
Magento Community
Magento Community
 
louisliu
Jr. Member
 
Total Posts:  7
Joined:  2009-06-09
 

Hi Tronics

Thank you for your full example.

Regards

Louis

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pradnya
Jr. Member
 
Total Posts:  12
Joined:  2009-05-20
 

Hello All,

Sorry for posting at the wrong place smile

Need some help on this......

I want to change the query that searches for products.
I found the page Query.php but this page just gets a single row from catalogsearch_query table.

On which page can i find the query where All the products are queried using FetchAll

Please help

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1630
Joined:  2009-06-23
 
Moshe - 27 December 2007 08:53 PM

I’m not sure what exactly you are trying to do, but here’s a way to run SQL query in Magento:

<?php

// fetch write database connection that is used in Mage_Core module
$write Mage::getSingleton('core/resource')->getConnection('core_write');

// now $write is an instance of Zend_Db_Adapter_Abstract
$write->query("insert into tablename values ('aaa','bbb','ccc')");

another safe way to do the custom query is:

$write Mage::getSingleton('core/resource')->getConnection('core_write');
$sql  "insert into tablename values (?, ?, ?)";
$write->query($sql, array('aaa','bbb','ccc'));

 Signature 

MagePsycho - Magento Freelancer
Free Modules: jQuery LightBoxes | Frontend Links Manager & more…
Commercial Modules: jQuery Sliders Pro | Custom Login Redirect Pro | Store Restrction Pro & more...

Follow me on Twitter @ magepsycho

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pradnya
Jr. Member
 
Total Posts:  12
Joined:  2009-05-20
 

Hello,

What exactly i want to do is to also allow ”plural search” when i try to search from the search box
eg: The result that i get after searching for “ Television” i should be getting the same result when i try searching for ”Televisions”.

I tried using this extension http://www.magentocommerce.com/extension/492/enhanced-search
But dosent seem to work for me.  :(

Any ideas??

 
Magento Community Magento Community
Magento Community
Magento Community
 
caiohn
Jr. Member
 
Total Posts:  7
Joined:  2008-08-20
 

Hi all,

I have written this

class Australia_Model_Mysql4_Shipping_Carrier_Australiapost extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init(’au/postcode/b2b’, ‘postcode’);
}

public function getRegion($toPostCode)
{
$write = Mage::getSingleton(’core/resource’)->getConnection(’core_write’);
$write->query("select region from au_postcode_b2b where postcode = 821");
// echo get_class($write);
$row = $write->fetch();
return $row[’region’];
}
}

but I am getting this error message:

Fatal error: Call to undefined method Varien_Db_Adapter_Pdo_Mysql::fetch() in /home/edscoco/public_html/dev/app/code/community/Fontis/Australia/Model/Mysql4/Shipping/Carrier/Australiapost.php on line 26

if I uncomment the get_class line, I can see the Varien_Db_Adapter_Pdo_Mysql class assigned to the variable.
Any clue of what is wrong?

Thanks,
Caio

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sandy Khan
Jr. Member
 
Total Posts:  25
Joined:  2009-07-12
 

Thank you Tronics! You rock man!!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
merigold
Sr. Member
 
Total Posts:  207
Joined:  2009-04-22
 

Great work guys…
this thread helps me lot ..
cheers!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Enteractiva
Sr. Member
 
Avatar
Total Posts:  77
Joined:  2010-05-13
Dominican Republic
 

Hi,

I’m trying to execute a query on Magento 1.4 from the mentioned sample, it turns out that every time I try to display the select result it doesn’t appear anything on the page.

Any ideas?

 Signature 

Enteractiva: Web Development and Design Services
Website: http://www.enteractiva.net

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2
 
© Copyright Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
819302 users|760 users currently online|519712 forum posts