Yii Framework Forum: Integrity constraint violation: 1062 Duplicate entry 'member-2' for key 'PRIMARY' - Yii Framework Forum

Jump to content

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

Integrity constraint violation: 1062 Duplicate entry 'member-2' for key 'PRIMARY'

#1 User is offline   gvanto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 16-August 12

  Posted 16 August 2012 - 05:36 AM

I'm on page 199 of the Agile Web Dev book (which is great, must say) but now I've gotten stuck when I run my test i get this error:

Quote

[03:19:21][root@ps79740:/home/twu5r/public_html/tasker/protected/tests]#phpunit unit/ProjectTest.php
PHPUnit 3.6.12 by Sebastian Bergmann.

Configuration read from /home/twu5r/public_html/tasker/protected/tests/phpunit.xml

Time: 0 seconds, Memory: 11.50Mb

There was 1 error:

1) ProjectTest::testUserAccessBasedOnProjectRole
CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'member-2' for key 'PRIMARY'

/home/twu5r/public_html/dev3/framework/db/CDbCommand.php:354
/home/twu5r/public_html/dev3/framework/db/CDbCommand.php:1181
/home/twu5r/public_html/dev3/framework/web/auth/CDbAuthManager.php:264
/home/twu5r/public_html/tasker/protected/tests/unit/ProjectTest.php:54

FAILURES!
Tests: 6, Assertions: 9, Errors: 1.



I (think) have followed everything exactly as per the instructions. (all unit tests previously have passed).

First part of ProjectTest.php:
class ProjectTest extends CDbTestCase {
  
  public $fixtures = array(
      'projects'=>'Project',
      'users'=>'User',
      'projUsrAssign'=>':tbl_project_user_assignment',
      'projUserRole'=>':tbl_project_user_role',
  );
  
  public function testUserRoleAssignment() {
    $project = $this->projects('project1');
    $user = $this->users('user1');
    $this->assertEquals(1, $project->associateUserToRole('owner', $user->id));
    $this->assertEquals(1, $project->removeUserFromRole('owner', $user->id));
  }
  
  public function testIsInRole() {
    $row1 = $this->projUserRole['row1'];    
    Yii::app()->user->setId($row1['user_id']);    
    $project = Project::model()->findByPk($row1['project_id']);    
    $this->assertTrue($project->isUserInRole('member'));
  }
  
  public function testUserAccessBasedOnProjectRole() {
    $row1 = $this->projUserRole['row1'];
    Yii::app()->user->setId($row1['user_id']);
    $project = Project::model()->findByPk($row1['project_id']);
    
    //Assign php snippet bizRule
    $auth = Yii::app()->authManager;
    $bizRule = 'return isset($params["project"]) && $params["project"]->isUserInRole("member");';
    $auth->assign('member', $row1['user_id'], $bizRule); //line 44
    $params = array('project'=>$project);
    
    $this->assertTrue(Yii::app()->user->checkAccess('updateIssue', $params));
    $this->assertTrue(Yii::app()->user->checkAccess('readIssue', $params));
    $this->assertFalse(Yii::app()->user->checkAccess('updateProject', $params));
  }


My tbl_project_user_role.php file:

<?php

return array(
    'row1' => array(
        'project_id' => 2,
        'user_id' => 2,
        'role' => 'member',
    ),
);
?>


Looking at the error, it seems that it's trying to insert a record but that primary key is already been duplicated.

I've tried a couple of things:

- Have the fixtures file return an empty array:
This STILL causes same issue

- Deleted the (only) record from the tbl_project_user_role table
Still get same issue


I am not quite sure what I am missing here?

Any help much appreciated,

gvanto
0

#2 User is offline   gvanto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 16-August 12

Posted 17 August 2012 - 12:08 AM

OK I found the issue, the AuthAssignment table needs to be cleared of all records for the test to pass.

If a SQL query to do this can be run at the start of running the unit test that would be awesome.

Anyone know how to configure this 'pre-test' sequence?

Best,
gvanto
0

#3 User is offline   Andro_ 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-August 12

Posted 17 August 2012 - 06:39 AM

On page 201 there is a solution to your problem. Basically you have to to the same thing as with the tbl_project_user_role. Ad a fixture that returns an empty array and then add that fixture to the test.
0

#4 User is offline   gvanto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 16-August 12

Posted 18 August 2012 - 10:44 PM

Pffft, I was getting ahead of myself! Thanks!
0

#5 User is offline   azad6026 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-December 13

Posted 12 December 2013 - 08:05 PM

I am new to Yii I am in chapter 7 of the book(web application development with yii and php) and I managed to solve the problem of duplication an authassinment able.I hope it is a good solution for this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate .... by using isAssigned methos of authManager in assign method in ProjectUserForm.php model like this:

public function assign()
{
if($this->_user instanceof User)
{

//assign the user, in the specified role, to the project
$this->project->assignUser($this->_user->id, $this->role);
//add the association, along with the RBAC biz rule, to our RBAC hierarchy
$auth = Yii::app()->authManager;
$bizRule='return isset($params["project"]) && $params["project"]->allowCurrentUser("'.$this->role.'");';
if(!$auth->isAssigned($this->role,$this->_user->id))
$auth->assign($this->role,$this->_user->id, $bizRule);
return true;


}
else
{
$this->addError('username','Error when attempting to assign this user to the project.');
return false;
}

}

so in the if statement {if(!$auth->isAssigned($this->role,$this->_user->id))} , if the user has owner role assigned t them prior this,it won't be duplicated and project will be assigned to user as owner and they are also available to add different users by different roles to that specific project.
0

Share this topic:


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

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