php - Symfony3 / Doctrine - function: adding row with foreign key -


i starting adventure symfony3 (and php) , encountered problem.

at moment application supposed allow user crud projects. displaying works fine, have problem creating.

i have got 2 tables: users , projects. project table contains project data including project owner id of user. symfony docs managed map both tables:

part of "project" entity:

/**  * @orm\manytoone(targetentity="user", inversedby="projects")  * @orm\joincolumn(name="uid", referencedcolumnname="id")  */ private $uid;   /**  * set uid  *  * @param \appbundle\entity\user $uid  *  * @return project  */ public function setuid(\appbundle\entity\user $uid = null) {     $this->uid = $uid;      return $this; }  /**  * uid  *  * @return \appbundle\entity\user  */ public function getuid() {     return $this->uid; } 

part of 'user' entity:

private $projects;  public function __construct() {     $this->projects = new arraycollection(); } 

at point i'm able display full project info, i.e. project title + project owner details. problem starts implementation of basic function creating new project.

i following doc: http://symfony.com/doc/current/book/doctrine.html#saving-related-entities. thing - don't want create new user well, assign existing user new project. i've created new function in project controller:

/**  * @route("/projects/create/", name="project_create")  */ public function createproject() {     $project = new project();     $project->settitle('webproject');     $project->setdescription('asdfghjkgfdsadfghgfdsdfg');     $project->setlogo('asdf');     $project->setcreatedat(time());     $project->setdateline('-');      // relate product category     $project->setuid(1);      $em = $this->getdoctrine()->getmanager();     $em->persist($project);     $em->flush();      return new response(         'saved new product id: '.$project->getid()     ); } 

this function supposed add new project , set user id 1 owner of project. expect doesn't work. error:

catchable fatal error: argument 1 passed appbundle\entity\project::setuid() must instance of appbundle\entity\user, integer given, called in...

well, indeed, that's true. i've no idea how pass there instance of user it's id.

thanks!


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -