I don’t want to create an empty Profile table entry into the database even the user does exist.

I can save the first profile info with save(); but other attempts will show an exception. How can i do a saveOrUpdate() without first retrieving wheter profile entry already exists? (less sql query)

There is a mysql-statement for this: INSERT INTO … ON DUPLICATE KEY UPDATE …

second way is to try an insert which throws an exception on duplicate key and update the record in catch

try {


} catch (Exception $e) {



if you work with AR, try to find the relation (user has_one profile), if no relation exists the relation is null

$user = User::model()->findByPk($userId);

$profile = $user->profile;

if (!$profile) {