0

I have an array, and I need to check if one of his values are in my database. I tried with collections, but I wasn't successful. This is my array:

$ab = Array
(
    [0] => Array
        (
            [@attributes] => Array
                (
                    [diffgrid] => Product1
                    [msdatarowOrder] => 0
                )

            [TIMESTAMP] => 
            [DEPARTAMENTO] =>                            
            [CATEGORIA] =>                                         
            [SUBCATEGORIA] =>                                     
            [FABRICANTE] => 
            [CODIGO] => 
            [DESCRICAO] =>
            [DESCRTEC] =>
            [PARTNUMBER] =>
            [EAN] =>
            [GARANTIA] =>
            [PESOKG] =>
            [PRECOREVENDA] =>
            [PRECOSEMST] =>
            [DATAVALIDADEPRECO] =>
            [DISPONIVEL] => 
            [URLFOTOPRODUTO] => 
            [ESTOQUE] => 
            [NCM] =>
            [LARGURA] =>
            [ALTURA] =>
            [PROFUNDIDADE] =>
            [ATIVO] => 
            [SUBSTTRIBUTARIA] => 
            [ORIGEMPRODUTO] =>
            [ESTOQUEDISPONIVEL] =>
        )

    [1] => Array
        (
            [@attributes] => Array
                (
                    [diffgrid] => Produtos2
                    [msdatarowOrder] => 1
                )

            [TIMESTAMP] =>
            [DEPARTAMENTO] =>
            [CATEGORIA] =>                                         
            [SUBCATEGORIA] =>                                      
            [FABRICANTE] => 
            [CODIGO] => 
            [DESCRICAO] =>
            [DESCRTEC] => 
            [PARTNUMBER] =>
            [EAN] =>
            [GARANTIA] =>
            [PESOKG] =>
            [PRECOREVENDA] => 
            [PRECOSEMST] => 
            [DATAVALIDADEPRECO] =>
            [DISPONIVEL] =>
            [URLFOTOPRODUTO] =>
            [ESTOQUE] =>
            [NCM] =>
            [LARGURA] =>
            [ALTURA] =>
            [PROFUNDIDADE] =>
            [ATIVO] =>
            [SUBSTTRIBUTARIA] =>
            [ORIGEMPRODUTO] =>
            [ESTOQUEDISPONIVEL] =>
        )
)

I need to check if the value of [CODIGO] is already in my table, at column "id_product". What would be the best way?

Model/Promotion.php:

class Novapc_Allnations_Model_Promotion extends Mage_Core_Model_Abstract
{
    /**
     * Entity code.
     * Can be used as part of method name for entity processing
     */
    const ENTITY    = 'novapc_allnations_promotion';
    const CACHE_TAG = 'novapc_allnations_promotion';

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'novapc_allnations_promotion';

    /**
     * Parameter name in event
     *
     * @var string
     */
    protected $_eventObject = 'promotion';


    public function _construct()
    {
        parent::_construct();
        $this->_init('novapc_allnations/promotion');
    }

    protected function _beforeSave()
    {
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()) {
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }

    protected function _afterSave()
    {
        return parent::_afterSave();
    }


    public function getDefaultValues()
    {
        $values = array();
        $values['status'] = 1;
        return $values;
    }

}

Model/Resource/Promotion.php:

class Novapc_Allnations_Model_Resource_Promotion extends Mage_Core_Model_Resource_Db_Abstract
{

    /**
     * constructor
     *
     * @access public
     * @author .
     */
    public function _construct()
    {
        $this->_init('novapc_allnations/promotion', 'entity_id');
    }
}

Model/Resource/Promotion/Collection.php:

class Novapc_Allnations_Model_Resource_Promotion_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected $_joinedFields = array();

    protected function _construct()
    {
        parent::_construct();
        $this->_init('novapc_allnations/promotion');
    }

    protected function _toOptionArray($valueField='entity_id', $labelField='name', $additional=array())
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }


    protected function _toOptionHash($valueField='entity_id', $labelField='name')
    {
        return parent::_toOptionHash($valueField, $labelField);
    }

    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}

And this is my table:

table

  • can you please add your database model from which you will get collection? – Abhishek Panchal Aug 25 '17 at 13:55
  • Done. (I guess? tell me if you need anything else, please) – Stordon Aug 25 '17 at 14:07
  • what will you get in CODIGO and in id_product ? – Abhishek Panchal Aug 25 '17 at 14:09
  • 7 digits number – Stordon Aug 25 '17 at 14:10
  • if its unique value then you can use addFieldToFilter() function – Abhishek Panchal Aug 25 '17 at 14:20
1
foreach($ab as $x){
     if($x['CODIGO'] != NULL){
         $model = Mage::getModel('novapc_allnations/promotion')->addFieldToFilter('id_product', $x['CODIGO']);
         var_dump($model->getData());
    }
}

Hope it helps you to get record from DB.

  • is it working ??? – Abhishek Panchal Aug 25 '17 at 14:46
  • Sorry for taking too long. Yes, it worked, thank you – Stordon Aug 25 '17 at 18:56
  • Glad that it worked for you – Abhishek Panchal Aug 25 '17 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.