1

Created flat table model that have foreign dependency for customer_entity.customer_id column, and for some unknown reason I am unable to delete object that I have successfull created before:

                 $required_model = Mage::getModel('Mymodule/Payments')
                    ->getCollection()
                    ->addFieldToSelect('customer_id')
                    ->addFieldToSelect('email')
                    ->addFieldToFilter('customer_id', $session->getCustomer()->getId())
                    ->getItems();

                if(is_array($required_model) && (count($required_model) > 0)) {

                    try {

                        reset($required_model);
                        Mage::app();
                        Mage::app('admin');
                        Mage::setIsDeveloperMode(true);
                        $model = $required_model[key($required_model)];
                        $model->delete();
                        var_dump($model); // object does not delete from table, and succesfully var_dumps here
                       }
                       ...

var_dump succesfully dumps the object:

                    object(Mycomp_Mymodule_Model_Payments)[175]
                      protected '_eventPrefix' => string 'core_abstract' (length=13)
                      protected '_eventObject' => string 'object' (length=6)
                      protected '_resourceName' => string 'Mymodule/payments' (length=12)
                      protected '_resource' => null
                      protected '_resourceCollectionName' => string 'Mymodule/payments_collection' (length=23)
                      protected '_cacheTag' => boolean false
                      protected '_dataSaveAllowed' => boolean true
                      protected '_isObjectNew' => null
                      protected '_data' => 
                        array (size=2)
                          'customer_id' => string '215' (length=3)
                            ...

my module config.xml:

            <mymodule_resource>
                <class>Mycomp_Mymodule_Model_Resource</class>
                <entities>
                    ...
                    <payments>
                        <table>mymodule_customer_payments_info</table>
                    </payments>
                 </entities>
                    ...
            </mymodule_resource>        

resource, collection and model files are set and initialized in constructor

where to dig ? thanks!

ps:

teble skeleton is like this:

    try {
        $sql = "
          CREATE TABLE `{$this->getTable('Mymodule/payments')}` (
            `entity_id` int(10) unsigned NOT NULL auto_increment,
            `country` text NOT NULL,
            `email` text NULL,
            `vat` text NULL,
            `customer_id` int(10) unsigned,
            `type` int(10) unsigned,
            `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (`entity_id`),
            CONSTRAINT `FK_mymodule_customer_payments_info_customer_id` FOREIGN KEY (`customer_id`) REFERENCES {$this->getTable('customer_entity')} (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE)
          ENGINE = InnoDB;";
        $this->run($sql);
    }

1 Answer 1

0

With the lots of debuggin and help of

lib/Varien/Db/Adapter/Pdo/Mysql.php

$_debug = true; 

figured out that solution was quite easy I should addFieldToSelect primary key too, and it is deletes successfully! so, i changed this:

    $required_model = Mage::getModel('Mymodule/Payments')
                    ->getCollection()
                    ->addFieldToSelect('customer_id')
                    ->addFieldToSelect('email')
                    ->addFieldToFilter('customer_id', $session->getCustomer()->getId())
                    ->getItems();

to this:

    $required_model = Mage::getModel('Mymodule/Payments')
                    ->getCollection()
                    ->addFieldToSelect('*')
                    ->addFieldToFilter('customer_id', $session->getCustomer()->getId())
                    ->getItems();

and thus primary key was succesfully retreived and deletion completed well !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.