Checking Whether A Field Value Exists In Table

Hi All,

I have a simple requirement. when i’m updating my_model i’m deleting the record for my_model table and again updating(as it has a foreign key referencing some other model)

when i’m creating a record for my_model i’m creating some entries into movies table(but my_model and movies are not having any relation to each other) . now while updating my_model, I dont want to delete the records in movies table rather i’d like to check if the values exists in movies table. if they exists I want to pick the id from that record and insert into sub_movies table.

can any one pls tell me how to achieve this


CActiveRecord::exists ?

Something like this?

$model = Movie::model()->find( $criteria )

if($model !== null) {

$subMovie = new SubMovie;

$subMovie->movie_id = $model->id;



Thanks all for the idea… I’ll try it out. hope that it works :)

That looks similar to what the CActiveRecord::exists method does, so if you find yourself using it more than once, use that instead. :)

Very true!

However thats a different usecase jacmoe. Because developer! wants to use the actual model id that the find() returns. If you’d use exists() in the if statement, you’d have to do another query inside the if brackets.

Thanks all for the reply.

I’m finding the value in movies table by using something like this

$object=Movies::model-> findbytributes() and getting the Id by $object->id. Using this value for my submovies table.

Thanks everyone