Yii Framework Forum: Sqlsrv :: Guid :: Conversion Failed When Converting From A Character String To Uniqueidentifier - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sqlsrv :: Guid :: Conversion Failed When Converting From A Character String To Uniqueidentifier Rate Topic: ***** 1 Votes

#1 User is offline   Martin Pascualon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 07-July 12

Posted 13 November 2013 - 09:19 PM

Hi, im new in yii, and i am tryin to use an existing sql server database with yii. I could create the models,controllers,and views but, when i try to save a new object with a uniqueidentifier type pk, the system says:

Conversion failed when converting from a character string to uniqueidentifier

I did validate an example : {33EF43BE-1D79-0148-831F-462752D033CD} and is ok.

This is part of my Model's code:

public function beforeSave() {


if (!$this->ContactoGUID) {

$this->ContactoGUID = $this->guid();

//$this->owner->guid = $guid->guid;
}
}
function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}

Can anybody give me a hand with this?

Thanks in advance,
Best Regards
Martin
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 519
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 14 November 2013 - 01:56 AM

1. when overriding beforeSave() you have to call the parent implementation and you need to return the result of that.
return parent::beforeSave();

2. In which line of the code does the error occur?
0

#3 User is offline   Martin Pascualon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 07-July 12

Posted 14 November 2013 - 06:26 AM

Hi CeBe,

did you mean this:

public function beforeSave() {

if(parent::beforeSave()){
if (!$this->ContactoGUID) {

$this->ContactoGUID = $this->guid();

}
//die('>>>'.$this->ContactoGUID);
return true;

}else{
return false;

}

}


The error still there. C:\wamp\www\yii-1.1.13\framework\db\CDbCommand.php(357)
throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',

and happend when i executed

if($model->save()){


Thanks for your repply
Regards
Martin
0

#4 User is offline   Martin Pascualon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 07-July 12

Posted 18 November 2013 - 09:46 AM

Solved !!

i just change the property type from String to uniqueidentifier and then i make a validation in beforeSave().

Thanks anyway CeBe
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users