In this example, I will show you how easy it is to retrieve a single piece of Data from your database. For this post, we 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 the need to compare if a string value matches a value in your database. To achieve this, you will need to know how to retrieve data from your 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');
Let’s set up a variable that we will use in our object to retrieve data from the correct table.
$myItem = Mage::getModel('foo_bar/item');
Now let’s 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 its value to our variable $prodid.
Now that we checked our table for a matching value you can simply set up an if statement to set up some rules.
Example:
if($prodid->count()>0) {
//Do something here
}
I hope this article was helpful and helped someone out there. If this post helped you please be sure to leave a comment below.