CModel->delete();

hello,

i’m trying to delete a record using a post method like you saw in the blog tutorial. my app is deleting the record but is also giving me an error.

for debugging i’m printing the record first before i delete it.

info: the primary key in the table Daytrip is IdDaytrip

thank you

view:


<?php echo CHtml::linkButton('Verwijder', array('submit' => array('daytrip/delete', 'IdDaytrip' => $daytrip->IdDaytrip), 'confirm' => "Are you sure to delete this post?")); ?>

controller:


    public function actionDelete() {

        if(Yii::app()->request->isPostRequest)

        {

            $dt = $this->loadDaytrip()

            print_r($dt); //this will print the correct record

            $dt->delete();

            $this->redirect(array('daytrip/list'));

        }

        else

            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

    }


    protected function loadDaytrip($id = null) {

        if($this->_daytrip === null)

        {

            if($id !== null || isset($_GET['IdDaytrip']))

                $this->_daytrip = Daytrip::model()->findbyPk($id !== null ? $id : $_GET['IdDaytrip']);

        }

        return $this->_daytrip;

    }

error:




CException

Omschrijving


Property "Daytrip.id" is not defined.

Bronbestand


/home/bas/projects/yii.dev/private_html/framework/db/ar/CActiveRecord.php(106)


        00094:      */

00095:     public function __get($name)

00096:     {

00097:         if(isset($this->_attributes[$name]))

00098:             return $this->_attributes[$name];

00099:         else if(isset($this->getMetaData()->columns[$name]))

00100:             return null;

00101:         else if(isset($this->_related[$name]))

00102:             return $this->_related[$name];

00103:         else if(isset($this->getMetaData()->relations[$name]))

00104:             return $this->getRelated($name);

00105:         else

00106: return parent::__get($name);

00107:     }

00108: 

00109:     /**

00110:      * PHP setter magic method.

00111:      * This method is overridden so that AR attributes can be accessed like properties.

00112:      * @param string property name

00113:      * @param mixed property value

00114:      */

00115:     public function __set($name,$value)

00116:     {

00117:         if($this->setAttribute($name,$value)===false)

00118:         {

        


Stack Trace


        #0 /home/bas/projects/yii.dev/private_html/framework/db/ar/CActiveRecord.php(106): CComponent->__get('id')

#1 /home/bas/projects/yii.dev/private_html/protected/back-end/models/Daytrip.php(115): CActiveRecord->__get('id')

#2 /home/bas/projects/yii.dev/private_html/framework/db/ar/CActiveRecord.php(1028): Daytrip->afterDelete()

#3 /home/bas/projects/yii.dev/private_html/protected/back-end/controllers/DaytripController.php(124): CActiveRecord->delete()

#4 /home/bas/projects/yii.dev/private_html/framework/web/actions/CInlineAction.php(32): DaytripController->actionDelete()

#5 /home/bas/projects/yii.dev/private_html/framework/web/CController.php(300): CInlineAction->run()

#6 /home/bas/projects/yii.dev/private_html/framework/web/filters/CFilterChain.php(129): CController->runAction(Object(CInlineAction))

#7 /home/bas/projects/yii.dev/private_html/framework/web/filters/CFilter.php(41): CFilterChain->run()

#8 /home/bas/projects/yii.dev/private_html/framework/web/CController.php(957): CFilter->filter(Object(CFilterChain))

#9 /home/bas/projects/yii.dev/private_html/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#10 /home/bas/projects/yii.dev/private_html/framework/web/filters/CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))

#11 /home/bas/projects/yii.dev/private_html/framework/web/CController.php(283): CFilterChain->run()

#12 /home/bas/projects/yii.dev/private_html/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#13 /home/bas/projects/yii.dev/private_html/framework/web/CWebApplication.php(320): CController->run('delete')

#14 /home/bas/projects/yii.dev/private_html/framework/web/CWebApplication.php(120): CWebApplication->runController('daytrip/delete/...')

#15 /home/bas/projects/yii.dev/private_html/framework/base/CApplication.php(135): CWebApplication->processRequest()

#16 /home/bas/projects/yii.dev/public_html/admin.php(11): CApplication->run()

#17 {main}        



Did you override afterDelete in protected/back-end/models/Daytrip.php? Not the intended model?

/Tommy

nice catch:


    protected function afterDelete() {

        $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->id}")->execute();

        $this->dbConnection->createCommand("DELETE FROM DaytripRubriek WHERE DaytripId = {$this->id}")->execute();

    }

changed $this->id to $this->IdDaytrip

working perfectly now! on last thing using the beforeSave and afterSave in the same model is not working for me?

so is there a solution to do this:


    protected function beforeSave()

    {

        $this->ZipCode = str_replace(' ', '', $this->ZipCode);

        $this->Phone = str_replace(' ', '', $this->Phone);

        $this->Fax = str_replace(' ', '', $this->Fax);

    }

Cache the current id with a beforeDelete() method:




    private $cachedId;


    protected beforeDelete() {

        $this->cachedId = $this->id;

    }


    protected function afterDelete() {

        $this->dbConnection->createCommand("DELETE FROM DaytripTag WHERE DaytripId = {$this->cachedId}")->execute();

        $this->dbConnection->createCommand("DELETE FROM DaytripRubriek WHERE DaytripId = {$this->cachedId}")->execute();

    }



That should do it.

Or probably even better: Delete the related records in beforeDelete() instead of afterDelete()

i think afterDelete is better, because when the delete action of the primary record fails you will lose al the child records…


    protected function beforeSave()

    {

        $this->ZipCode = str_replace(' ', '', $this->ZipCode);

        $this->Phone = str_replace(' ', '', $this->Phone);

        $this->Fax = str_replace(' ', '', $this->Fax);

        return parent::beforeSave();

    }

Unless you use transactions.