2016-11-23 2 views
1

Ich arbeite mit Symfony und Doctrine. Wenn das Schema über den Konsolenbefehl php bin/console doctrine:schema:update --force erstellt wird, erstellt es nicht die Relationen, die ich möchte.Doktrin - Relation ist nicht in der Datenbank erstellt

Zwei Klassen:

Kandidaten ORM Entity Klasse:

namespace AppBundle\Libraries\Data; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="candidates") 
*/ 
class Candidates 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    * @ORM\OneToMany(targetEntity="AppBundle\Libraries\Data\Votes", mappedBy="nCandidate") 
    */ 
    private $nID; 

    /** 
    * Get nID 
    * 
    * @return integer 
    */ 
    public function getNID() 
    { 
     return $this->nID; 
    } 
} 

Stimmen ORM Entity Klasse:

namespace AppBundle\Libraries\Data; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="votes") 
*/ 
class Votes 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    */ 
    private $nID; 

    /** 
    * @ORM\Column(type="integer") 
    * @ORM\ManyToOne(targetEntity="AppBundle\Libraries\Data\Candidates", inversedBy="nID") 
    */ 
    private $nCandidate; 

    /** 
    * Get nID 
    * 
    * @return integer 
    */ 
    public function getNID() 
    { 
     return $this->nID; 
    } 

    /** 
    * Set nCandidate 
    * 
    * @param integer $nCandidate 
    * 
    * @return Votes 
    */ 
    public function setNCandidate($nCandidate) 
    { 
     $this->nCandidate = $nCandidate; 

     return $this; 
    } 

    /** 
    * Get nCandidate 
    * 
    * @return integer 
    */ 
    public function getNCandidate() 
    { 
     return $this->nCandidate; 
    } 
} 

Die Lehre Config-Abschnitt in config.yml Aussehen so:

# Doctrine Configuration 
doctrine: 
    dbal: 
     driver: pdo_mysql 
     host:  "%database_host%" 
     port:  "%database_port%" 
     dbname: "%database_name%" 
     user:  "%database_user%" 
     password: "%database_password%" 
     charset: UTF8 
     # if using pdo_sqlite as your database driver: 
     # 1. add the path in parameters.yml 
     #  e.g. database_path: "%kernel.root_dir%/data/data.db3" 
     # 2. Uncomment database_path in parameters.yml.dist 
     # 3. Uncomment next line: 
     #  path:  "%database_path%" 

    orm: 
     auto_generate_proxy_classes: "%kernel.debug%" 
     naming_strategy: doctrine.orm.naming_strategy.underscore 
     auto_mapping: false 
     mappings: 
      FOSUserBundle: ~ 
      AppBundle: 
       type: annotation 
       dir: "%kernel.root_dir%/../src/AppBundle/Libraries/Data" 
       prefix: AppBundle\Libraries\Data 

Die Relationen, die erstellt werden sollen, sind in PHPMyadmin oder MySQL Workbench nicht sichtbar.

+0

Doctrine Meta-Daten-Cache aktiviert ist? – Vamsi

+0

@Vamsi Ich habe nichts speziell aktiviert, ist das in der Konfig? – Orlando

+0

Haben Sie einen Fehler oder wurde nichts erstellt? Was bedeutet Ausgabebefehl php bin/console doctrine: Schema: update --dump-sql? – Thomas

Antwort

0

Die ID-Spalte jeder Tabelle $nID die Eigenschaftsnamen hatte, aber es hat $id sein.

Ich entfernte auch den @ORM\Column(type="integer") Kommentar über jeder Eigenschaft mit einer @ORM\ManyToOne(targetEntity="Class", inversedBy="ID") Beziehung zu einem ID Primärschlüssel.

1

Auch wenn Sie diese Entitäten in Nicht-Standard-Verzeichnis abgelegt haben, die Symfony erwartet, haben Sie konfiguriert Lehre ORM richtig durch benutzerdefinierte Zuordnung Einstellung ..

Die Frage erscheint wegen des Mangels an Primärschlüssel in den Abstimmungen zu sein Entität.

Jede Entität muss einen Bezeichner/Primärschlüssel haben.

Einheit Stimmen mit * @ORM\Id

AppBundle\Libraries\Data\Votes

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


    /** 
    * @return mixed 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @param mixed $id 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 
    } 

Edit: Dies ist die SQL Ich habe (MySQL), wenn ich

php app/console doctrine:schema:update --dump-sql 

CREATE TABLE candidates (
    n_id INT AUTO_INCREMENT NOT NULL 
    ,PRIMARY KEY (n_id) 
    ) DEFAULT CHAR 

SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB; 

CREATE TABLE votes (
    id INT AUTO_INCREMENT NOT NULL 
    ,n_candidate INT NOT NULL 
    ,PRIMARY KEY (id) 
    ) DEFAULT CHAR 

SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB; 
+0

Ich habe eine ID-Spalte hinzugefügt, aber die Beziehung ist immer noch nicht dort – Orlando

+1

hast du den Cache gelöscht? php app/console cache: löschen und php app/console doctrine: cache: clear-metadata – Vamsi

+0

Hat nicht geholfen, relationen werden nicht erzeugt – Orlando

0

Versuchen Sie Folgendes ausführen:

Candidate Einheit

Vote Einheit

namespace AppBundle\Libraries\Data; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="votes") 
*/ 
class Votes 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    */ 
    private $nID; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    private $nCandidate; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Libraries\Data\Candidates", inversedBy="vote") 
    */ 
    private $candidate; 
    /** 
    * Get nID 
    * 
    * @return integer 
    */ 
    public function getNID() 
    { 
     return $this->nID; 
    } 

    /** 
    * Set nCandidate 
    * 
    * @param integer $nCandidate 
    * 
    * @return Votes 
    */ 
    public function setNCandidate($nCandidate) 
    { 
     $this->nCandidate = $nCandidate; 

     return $this; 
    } 

    /** 
    * Get nCandidate 
    * 
    * @return integer 
    */ 
    public function getNCandidate() 
    { 
     return $this->nCandidate; 
    } 
} 

Dann php bin/console doctrine:schema:update --dump-sql laufen, um zu sehen, ob Lehre Kenntnis von der Beziehung ist

+0

Das wirft einen Fehler:' [Documen \ ORM \ ORMException] Spaltenname 'id' referenziert für Relation von AppBundle \ Libraries \ Data \ Votes zu AppBundle \ Libraries \ Data \ Candidates existiert nicht. "Wenn ich den Konsolenbefehl ausführe. Wenn ich '@ORM \ Column (type =" integer ")' über der Relation hinzufügt, erkennt es die Änderung, erstellt aber keine Relation. – Orlando

Verwandte Themen