Checking Db For Entry

I found couple examples online, how to add criteria to Yii model(), but I cant figure out why mine doesnt work. It just makes new entry in db, but I want if an entry exists with have specific zoneID and carID to update it. All $_POST variables are valid since I cant save a new entry.


function actionSaveZone()

        {

            $criteria=new CDbCriteria();

            $criteria->condition = 'car_type_zones_id=:zoneID AND car_type_view_id=:carID';

            $criteria->params = array(':zoneID'=>$_POST['zoneID'], ':carID'=>$_POST['carID']);

            if( EeCarTypeViewPoints::model()->exists($criteria))

            {

                $model = EeCarTypeViewPoints::model()->findByAttributes( array('car_type_zones_id'=>$_POST['zoneID'],'car_type_view_id'=>$_POST['carID']));

            }

            else

            {

                $model = new EeCarTypeViewPoints;

            }

            $model->car_type_view_id = $_POST['carTypeID'];

            $model->car_type_zones_id = $_POST['zoneID'];

            $model->car_type_view_point_coord = $_POST['coords'];

            $model->car_type_id = $_POST['carID'];

            $model->save();

        }

Where is my mistake here?

Moved from General Discussion for Yii 2.0 to General Discussion for Yii 1.1.x.

This is a strange approach. You’re not even using the CDbCriteria to fetch the model.

Fetch the model with the CDbCriteria, then make your changes and save that model:




function actionSaveZone()

        {

            $criteria=new CDbCriteria();

            $criteria->condition = 'car_type_zones_id=:zoneID AND car_type_view_id=:carID';

            $criteria->params = array(':zoneID'=>$_POST['zoneID'], ':carID'=>$_POST['carID']);


            $model = EeCarTypeViewPoints::model()->find($criteria);


            if ($model === null)

                    $model = new EeCarTypeViewPoints;


            $model->car_type_view_id = $_POST['carTypeID'];

            $model->car_type_zones_id = $_POST['zoneID'];

            $model->car_type_view_point_coord = $_POST['coords'];

            $model->car_type_id = $_POST['carID'];

            $model->save();

        }



If you’re not successfully retrieving and updating the model, there’s something wrong the criteria you’re using.

Nevertheless, you should look through the Yii Guide for better ways of handling forms.

EDIT:

In fact, you don’t even need the criteria, just use findByAttributes() and get rid of the initial existence check.




function actionSaveZone()

        {

            $model = EeCarTypeViewPoints::model()->findByAttributes(array(

                'car_type_zones_id'=>$_POST['zoneID'],

                'car_type_view_id'=>$_POST['carID'],

            ));


            if ($model === null)

                    $model = new EeCarTypeViewPoints;


            $model->car_type_view_id = $_POST['carTypeID'];

            $model->car_type_zones_id = $_POST['zoneID'];

            $model->car_type_view_point_coord = $_POST['coords'];

            $model->car_type_id = $_POST['carID'];

            $model->save();

        }



Thank you so much, you are my hero, for sure will check out that guide.