I am working on creating models for a module I'm developing but I've run into a problem echoing out the result from a query.
What I get when using a var_dump()
calling the the model in the block is NULL
I don't understand because in the resource model, if i do an echo $select
it prints out the query which I enter into phpMyAdmin and it find the row. I think i must be trying to output the row wrongly.
This is my resource model:
class MyCompany_Facebook_Model_Resource_Facebookcoupon extends Mage_Core_Model_Resource_Db_Abstract
{
protected function _construct()
{
$this->_init('facebook/facebookcoupon', 'entity_id');
}
public function loadByField($field,$value)
{
$table = $this->getTable('facebook/facebookcoupon');
$where = $this->_getReadAdapter()->quoteInto("$field = ?", $value);
$select = $this->_getReadAdapter()->select()->from($table,array('facebook_id'))->where($where);
$id = $this->_getReadAdapter()->fetchOne($select);
return $id;
}
This is my model
class MyCompany_Facebook_Model_Facebookcoupon extends Mage_Core_Model_Abstract
{
protected function _construct()
{
parent::_construct();
$this->_init('facebook/facebookcoupon');
}
public function loadByField($field,$value)
{
$id = $this->getResource()->loadByField($field,$value);
$this->load($id);
}
}
and i call it using this block
class MyCompany_Facebook_Block_Content extends Mage_Core_Block_Template
{
private $couponCode;
public function displayCoupon($test)
{
$facebookid = Mage::getModel('facebook/facebookcoupon')->loadByField('facebook_id', '14547854');
var_dump($facebookid);
Adrock.use the below for more suitable solution
$model = Mage::getModel('facebook/facebookcoupon') ->getCollection()
->addFieldToFilter('facebook_id', 14547854) ->getFirstItem();
// here you'll get a collection but single record -
Please note:
loadByField($field,$value) in resource model is wrong.you can use load()
function only whenever,you will be trying to fetch data using primary key.