Sevenbits
Sevenbits

Reputation: 3

Symfony 4.1 and Doctrine asossiation Problems

I read a lot Postings on Stackoverflow and the doctrine Documentation but i cannot find the error in this code. Maybe someone can lead me to the right direction as i'm sure i don't get the point on this concept. This is my first Project with doctrine.

I have three entities and the Database is generated correct but i always get these Errors when doing

php ./bin/console doctrine:schema:validate

the entities are as follows (shortened):

[FAIL] The entity-class App\Entity\DeployedTrap mapping is invalid: * The association App\Entity\DeployedTrap#trapId refers to the inverse side field App\Entity\TrapDefinition#id which is not defined as association. * The association App\Entity\DeployedTrap#trapId refers to the inverse side field App\Entity\TrapDefinition#id which does not exist. * The association App\Entity\DeployedTrap#customer refers to the inverse side field App\Entity\Customer#customerId which is not defined as association. * The association App\Entity\DeployedTrap#customer refers to the inverse side field App\Entity\Customer#customerId which does not exist.

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 *
 * @ORM\Table(name="traps_deployed")
 * @ORM\Entity(repositoryClass="App\Repository\DeployedTrapRepository")
 */
class DeployedTrap
{
    /**
     * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * Owning side
     * @ORM\JoinColumn(name="trap_id", referencedColumnName="id")
     * @ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="id")
     */
    public $trapId;

    /**
     * @ORM\JoinColumn(name="customer", referencedColumnName="customerId")
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="customerId")
     */
    private $customer;




}

Class TrapDefinition:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Table(name="traps_definition")
 * @ORM\Entity(repositoryClass="App\Repository\TrapDefinitionRepository")
 */
class TrapDefinition
{
    /**
     * @ORM\Column(name="id",type="integer", options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapId")
     */
    public $id;





    public function __construct()
    {
        $this->id = new ArrayCollection();
    }


}

class Customer:

    namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Events;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Table(name="customers")
 * @ORM\Entity(repositoryClass="Doctrine\ORM\EntityRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Customer
{
    /**
     * @ORM\Column(type="integer",name="customerId",length=11, nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="customer")
     */
    public $customerId;




    public function __construct()
    {
        $this->customerId = new ArrayCollection();

    }


}

Upvotes: 0

Views: 540

Answers (1)

Jim Panse
Jim Panse

Reputation: 2270

First: Forget the native database approach for linking Models via Id's (see $trapId in your DeployerTrap) in Doctrine. You will always link objects/collections to each other so $trapId should be $trapDefinition.

And that is your main problem here

Just add a field $deployerTraps to your TrapDefinition class

/**
* @ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapDefinition")
*/
public $deployerTraps;

And rename the $trapId to $trapDefinition in your DeployedTrap class

/**
 * @ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="deployerTraps")
 */
public $trapDefinition;

Upvotes: 2

Related Questions