Yii Framework Forum: Bug: Constraint is not resolved correctly in 1.1-dev - Yii Framework Forum

Jump to content

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

Bug: Constraint is not resolved correctly in 1.1-dev Rate Topic: -----

#1 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 14 July 2009 - 08:15 PM

Hello, I have found the following Bug in the 1.1-dev tree:

I use the following two models:


CREATE TABLE  `Customer` (
  `customerid` int(10) unsigned NOT NULL auto_increment COMMENT 'Kundennummer',
`name` varchar(255) NOT NULL,
PRIMARY KEY  (`customerid`),
  KEY `fk_Customer_Users` (`User`),
  CONSTRAINT `fk_Customer_Users` FOREIGN KEY (`User`) REFERENCES `Users` (`idUsers`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB


And


CREATE TABLE  `Object` (
  `objectid` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `customerid` int(10) unsigned NOT NULL)
PRIMARY KEY  (`objectid`),
  KEY `fk_object_customer` (`customerid`),
  CONSTRAINT `fk_object_customer` FOREIGN KEY (`customerid`) REFERENCES `Customer` (`customerid`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB


So there is a 1:n relation. Yii perfectly corrects sets the relations when creating the models:


public function relations()
{

return array(
'customer' => array(self::BELONGS_TO, 'Customer', 'customerid'),
);
}



public function relations()
{
return array(
'objects' => array(self::HAS_MANY, 'Object', 'customerid'),
);
}


Now, after inserting some random data to customer, i try to insert a Object. It fails:


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ZimmerDienst/Object`, CONSTRAINT `fk_object_customer` FOREIGN KEY (`customerid`) REFERENCES `Customer` (`customerid`) ON DELETE NO ACTION ON UPDATE NO ACTION)


because in the ObjectController/actionCreate.php this line isn't working as expected:


$model->attributes=$_POST['Object'];


it works by appending this line:


$model->customerid = $_POST['Object']['customerid'];


so the automatic assignment of $model->attributes doesn't seem to work with foreign key definitions ?  Sorry for having a long breath for such a small problem; english is my foreign language, thank you.
0

#2 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 15 July 2009 - 03:16 AM

By default, primary keys are not safe to be massively assigned.

You have to define a 'safe' rule for the given PK.
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,875
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 July 2009 - 07:43 AM

In 1.1, massive assignments are based on rules() declaration. Only attributes declared in some rule can be massively assigned.
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