Orlando Orlando - 12 days ago 8
PHP Question

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.

Answer

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.