In this example I will show you how easy it is to retrieve a single piece of Data from you database. For the purposes of this post, we will assume you already have a working extension and just need to know how to retrieve a single piece of data from your controller.
Let’s pretend that you have need to compare if a string value matches a value in your database. Well in order to achieve this you will need to know how to retrieve data from you database.
Let’s assume we passed some data in our controller that contains a product id.
//More post coming soon on how to use getPost() and getParams()
$proddata = $this->getRequest()->getPost('product_id');
Lets setup a variable that we will use in our object to retrieve data from the correct table.
$myItem = Mage::getModel('foo_bar/item');
Now lets make this useful and retrieve a row in our table if it finds a match.
$prodid = $myItem->getCollection()
->addFieldToFilter('customer_id', $data['customer_id'])
->addFieldToFilter('product_id', $proddata);
Now this object is trying to see if it can find a match in our table that would be equal to $proddata which is the product_id and matches a customer id. If it finds a match it will assign it’s value to our variable $prodid.
Now that we checked our table for a matching value you can simply setup an if statement to setup some rules.
Example:
if($prodid->count()>0) {
//Do something here
}
I hope this article was useful and helped someone out there. If this post helped you please be sure to leave a comment below.
Good Luck!
Chris