1
votes

I have a system that took form information detailing a project, added it to a project table and is meant to add an entry into an assigned projects table to associate user with project (point of this is allowing multiple users for each project). Anyway I got this working without foreign keys, struggled to add them but eventually got them.

Unfortunately this additional has caused this error 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'projectId' cannot be null' whenever something is added to the assignedProjects table.

So my question is, have I missed something in my codes?

The code to add a new row to assignedProjects:

$assignedProject = new AssignedProjects();
$assignedProject->setProjectId($project->getId());
$assignedProject->setUserId($user[0]['id']);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($assignedProject);
$em->flush();

The code for the assignProjects entity: class AssignedProjects { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id;

/**
 * @var integer $projectId
 *
 * @ORM\Column(name="projectId", type="integer")
 */
private $projectId;

/**
* @ORM\ManyToOne(targetEntity="Projects", inversedBy="assignment")
* @ORM\JoinColumn(name="projectId", referencedColumnName="id")
*/
private $project;


/**
 * @var integer $UserId
 *
 * @ORM\Column(name="userId", type="integer")
 */
private $userId;

/**
* @ORM\ManyToOne(targetEntity="Dev\UserBundle\Entity\User", inversedBy="assignment")
* @ORM\JoinColumn(name="userId", referencedColumnName="id")
*/
private $user;

(followed by the usual getters and setters)

and the project tables entity is:

class Projects
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string $projectName
*
* @ORM\Column(name="projectName", type="string", length=255)
*/
private $projectName;


/**
* @ORM\OneToMany(targetEntity="AssignedProjects", mappedBy="project")
*/
protected $assignment;

Any help would be much appreciated!

2

2 Answers

0
votes

Either you use the ProjectId and UserId columns and manage the relationship manually (not recommended) or you use the doctrine relationships(recommended), but don´t do both things. If you go for the second option, don´t include the projectId and userId columns, they are automatically created for you by doctrine. So, your AssignedProjects class should be:

class AssignedProjects {
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="Projects", inversedBy="assignment")
 * @ORM\JoinColumn(name="projectId", referencedColumnName="id")
 */
private $project;


/**
 * @ORM\ManyToOne(targetEntity="Dev\UserBundle\Entity\User", inversedBy="assignment")
 * @ORM\JoinColumn(name="userId", referencedColumnName="id")
 */
private $user;

and in your controller you would do:

$assignedProject = new AssignedProjects();
$assignedProject->setProject($project);
$assignedProject->setUser($user[0]);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($assignedProject);
$em->flush();

Note that I am setting the Project and User fields, not the ids

By the way, unless you need to save extra data about this project assignement (things like the date or similar), you can declare a direct ManyToMany relationship between User and Project and do away with this class, Doctrine would generate the needed table by itself

0
votes

With Doctrine2, you don't have to declare the foreign key (projectId) but only the association (project). So you can delete $projectId property, as well as setProjectId ans getProjectId methods. Same fix for $user...

Instead, you will use setProject like that :

$assignedProject = new AssignedProjects();
$assignedProject->setProject($project);
$assignedProject->setUser($user[0]);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($assignedProject);
$em->flush();

Have a look to Doctrine2 documentation, it will help you, for sure ! http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html