简体   繁体   中英

Doctrine not creating one-to-many relationship on doctrine:schema:update

I have two entities User and Shout using annotations in the respective classes. User can have many shouts . For some reason when I create the annotations and then run bin/console doctrine:schema:update --dump-sql it does not show the relationship and when I run the actual update, it creates the tables but does not create the relationships. Here are the classes:

/**
 * Shout
 *
 * @ORM\Table(name="shouts")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ShoutRepository")
 * @package AppBundle\Entity
 */
class Shout
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="integer", nullable=false)
     * @ORM\ManyToOne(targetEntity="User", inversedBy="shouts")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @var string
     *
     * @ORM\Column(name="message", type="text")
     */
    private $message;
......

/**
 * User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @package AppBundle\Entity
 */
class User extends BaseUser
{

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

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Shout", mappedBy="users")
     */
    private $shouts;

    /**
     * User constructor.
     */
    public function __construct()
    {
        $this->shouts = new ArrayCollection();
        parent::__construct();
    }

... and the bin/console doctrine:schema:update --dump-sql

CREATE TABLE shouts (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, message LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE users (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL, username_canonical VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, email_canonical VARCHAR(255) NOT NULL, enabled TINYINT(1) NOT NULL, salt VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, last_login DATETIME DEFAULT NULL, locked TINYINT(1) NOT NULL, expired TINYINT(1) NOT NULL, expires_at DATETIME DEFAULT NULL, confirmation_token VARCHAR(255) DEFAULT NULL, password_requested_at DATETIME DEFAULT NULL, roles LONGTEXT NOT NULL COMMENT '(DC2Type:array)', credentials_expired TINYINT(1) NOT NULL, credentials_expire_at DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_1483A5E992FC23A8 (username_canonical), UNIQUE INDEX UNIQ_1483A5E9A0D96FBF (email_canonical), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

The annotations you using are wrong, you should use Column OR ManyToOne but not both.

class Shout
{
    // ...

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="shouts")
     */
    private $user;

    // ...
}

class User extends BaseUser
{

    // ...

    /**
     * @var ArrayCollection|Shout[]
     *
     * @ORM\OneToMany(targetEntity="Shout", mappedBy="user")
     */
    private $shouts;

    // ...
}

Note :

The @ORM\\JoinColumn(name="user_id", referencedColumnName="id") line is kind of useless as doctrine smart enough to figure out the column names and these are the default values. You should instead think about if the relationship is optional or not and use @ORM\\JoinColumn(onDelete="SET NULL", nullable=true) or @ORM\\JoinColumn(onDelete="CASCADE", nullable=false) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM