Orlando
Orlando

Reputation: 499

Doctrine - Relation is not created in database

I am working with Symfony and Doctrine. When the schema is created through the console command php bin/console doctrine:schema:update --force it does not create the relations, which I want it to.

Two Classes:

Candidates ORM Entity Class:

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;
    }
}

Votes ORM Entity Class:

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;
    }
}

The doctrine config section in config.yml looks like this:

# 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

The relations that should be created are not visible in PHPMyadmin or MySQL Workbench.

Upvotes: 2

Views: 1666

Answers (3)

Orlando
Orlando

Reputation: 499

The ID column of each table had the property name $nID, but it has to be $id.

I also removed the @ORM\Column(type="integer") comment above each property with a @ORM\ManyToOne(targetEntity="Class", inversedBy="ID") relation to an ID primary key.

Upvotes: 1

tchartron
tchartron

Reputation: 717

Try the following :

Candidate entity

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")
     */
    private $nID;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Libraries\Data\Votes", mappedBy="candidate")
     */
    private $vote;

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

Vote entity

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;
    }
}

Then run php bin/console doctrine:schema:update --dump-sql to see if doctrine is aware of the relation

Upvotes: 0

Vamsi Krishna B
Vamsi Krishna B

Reputation: 11500

Even though you have Placed those Entities in non standard directory that Symfony expects, you have configured Doctrine ORM correctly by setting custom mapping..

The issue appears to be because of the lack of primary key in the Votes Entity.

Every Entity must have an identifier/primary key.

Votes entity with * @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 : This is the SQL I got ( MySQL ) when I run

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;

Upvotes: 1

Related Questions