教条没有在学说上创建一对多的关系:schema:update

时间:2016-02-20 01:32:50

标签: php mysql symfony doctrine-orm doctrine

我有两个实体UserShout在相应的类中使用注释。 User可以有多个shouts。出于某种原因,当我创建注释然后运行bin/console doctrine:schema:update --dump-sql时,它不显示关系,当我运行实际更新时,它会创建表但不创建关系。以下是课程:

/**
 * 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();
    }

...和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;

1 个答案:

答案 0 :(得分:4)

您使用的注释错误,您应该使用ColumnManyToOne,但不能同时使用两者。

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;

    // ...
}

注意

@ORM\JoinColumn(name="user_id", referencedColumnName="id")行有点无用,因为学说足够聪明,可以找出列名,这些是默认值。您应该考虑关系是否可选,并使用@ORM\JoinColumn(onDelete="SET NULL", nullable=true)@ORM\JoinColumn(onDelete="CASCADE", nullable=false)