I don’t understand how to do so, Can you show me please? Basically I’m doing beforeSave() inside my Profile model which needs a variable (id) from my User model
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'user_id', table 'panasonic.dbo.profiles'; column does not allow nulls. INSERT fails.. The SQL statement executed was: INSERT INTO [dbo].[profiles] ([lastname], [firstname], [birthday], [user_id]) VALUES (:yp0, :yp1, :yp2, :yp3)
C:\xampp\framework\db\CDbCommand.php(354)
342 return $n;
343 }
344 catch(Exception $e)
345 {
346 if($this->_connection->enableProfiling)
347 Yii::endProfile('system.db.CDbCommand.execute('.$this->getText().')','system.db.CDbCommand.execute');
348 $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;
349 $message = $e->getMessage();
350 Yii::log(Yii::t('yii','CDbCommand::execute() failed: {error}. The SQL statement executed was: {sql}.',
351 array('{error}'=>$message, '{sql}'=>$this->getText().$par)),CLogger::LEVEL_ERROR,'system.db.CDbCommand');
352 if(YII_DEBUG)
353 $message .= '. The SQL statement executed was: '.$this->getText().$par;
354 throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',
355 array('{error}'=>$message)),(int)$e->getCode(),$errorInfo);
356 }
357 }
358
359 /**
360 * Executes the SQL statement and returns query result.
361 * This method is for executing an SQL query that returns result set.
362 * @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
363 * to {@link bindParam} and {@link bindValue}. If you have multiple input parameters, passing
364 * them in this way can improve the performance. Note that if you pass parameters in this way,
365 * you cannot bind parameters or values using {@link bindParam} or {@link bindValue}, and vice versa.
366 * binding methods and the input parameters this way can improve the performance.
Stack Trace
#0
– C:\xampp\framework\db\ar\CActiveRecord.php(1014): CDbCommand->execute()
1009 {
1010 Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord');
1011 $builder=$this->getCommandBuilder();
1012 $table=$this->getMetaData()->tableSchema;
1013 $command=$builder->createInsertCommand($table,$this->getAttributes($attributes));
1014 if($command->execute())
1015 {
1016 $primaryKey=$table->primaryKey;
1017 if($table->sequenceName!==null)
1018 {
1019 if(is_string($primaryKey) && $this->$primaryKey===null)
#1
– C:\xampp\framework\db\ar\CActiveRecord.php(787): CActiveRecord->insert(null)
782 * @return boolean whether the saving succeeds
783 */
784 public function save($runValidation=true,$attributes=null)
785 {
786 if(!$runValidation || $this->validate($attributes))
787 return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
788 else
789 return false;
790 }
791
792 /**
#2
– C:\xampp\htdocs\panasonic\protected\modules\user\controllers\AdminController.php(89): CActiveRecord->save()
84 $model->password=Yii::app()->controller->module->encrypting($model->password);
85 if($model->save()) {
86 $profile->user_id=$model->id;
87
88 var_dump($model->id);
89 $profile->save();
90 }
91
92
93
94 $this->redirect(array('view','id'=>$model->id));
#3
– C:\xampp\framework\web\actions\CInlineAction.php(50): AdminController->actionCreate()
45 $controller=$this->getController();
46 $method=new ReflectionMethod($controller, $methodName);
47 if($method->getNumberOfParameters()>0)
48 return $this->runWithParamsInternal($controller, $method, $params);
49 else
50 return $controller->$methodName();
51 }
52
53 }
#4
+ C:\xampp\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "user/admin/create"))
#5
+ C:\xampp\framework\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)
#6
+ C:\xampp\framework\web\filters\CFilter.php(41): CFilterChain->run()
#7
+ C:\xampp\framework\web\CController.php(1146): CFilter->filter(CFilterChain)
#8
+ C:\xampp\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)
#9
+ C:\xampp\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)
#10
+ C:\xampp\framework\web\CController.php(292): CFilterChain->run()
#11
+ C:\xampp\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))
#12
+ C:\xampp\framework\web\CWebApplication.php(276): CController->run("create")
#13
+ C:\xampp\framework\web\CWebApplication.php(135): CWebApplication->runController("user/admin/create")
#14
+ C:\xampp\framework\base\CApplication.php(162): CWebApplication->processRequest()
#15
– C:\xampp\htdocs\panasonic\index.php(13): CApplication->run()
08 defined('YII_DEBUG') or define('YII_DEBUG',true);
09 // specify how many levels of call stack should be shown in each log message
10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
11
12 require_once($yii);
13 Yii::createWebApplication($config)->run();
14
Not sure if you want to see the rest.
$model->save performs correctly, my info is there and on my view. If I want to view a specific user it displays with an error because Profile is not being saved.
I tried var_dump but it doesn’t work. The interesting thing is if I omit profile->save the info is saved on $model->save but I get a 404 error for my redirection. Even though if I got to the page manually it displays with this error
Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\panasonic\protected\modules\user\views\admin\view.php on line 30
Referring to a code that calls $profile.
Thanks for your reply