【问题标题】:Update failed in Symfony 3.1Symfony 3.1 更新失败
【发布时间】:2017-01-06 18:16:51
【问题描述】:

我正在使用 Symfony 3.1 中名为 ProfileBlog 的两个实体。这里这两个实体构成了一对多的关系。一个 Profile 可以创建多个 Blog ,除了一个 Blog 属于一个 Profile 。因此,要从Blog 访问个人资料信息,我想在blog table 中创建一个foreign key。我已经在我的代码中做到了,但主要问题是当我写这个命令时——$ php bin/console doctrine:schema:update --dump-sql,它显示 “无需更新 - 您的数据库已与当前实体元数据同步”。任何人都可以帮助我...这里我提供我完成的两个类...

//Profile.php

<?php
class Profile {
/**
 * @var int
 */
private $id;

/**
 * @var string
 *
 */
private $userName;

/**
 * @var string
 *
 */
private $email;

/**
 * @ORM\Column(name="url", type="string")
 */
private $url;

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

/**
 * Set userName
 *
 * @param string $userName
 *
 * @return Profile
 */
public function setUserName($userName)
{
    $this->userName = $userName;

    return $this;
}

/**
 * Get userName
 *
 * @return string
 */
public function getUserName()
{
    return $this->userName;
}

/**
 * Set email
 *
 * @param string $email
 *
 * @return Profile
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string
 */
public function getEmail()
{
    return $this->email;
} }

博客.php

类博客 { /** * @var 整数 */ 私人 $id;

/**
 * @var string
 */
private $blogTitle;

/**
 * @var string
 */
private $blogContent;

/**
 *
 *      >@ORM\ManyToOne(targetEntity="AppBundle\Entity\Profile",inversedBy="blogs")
 *   @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
 *
 */
private $product_id;


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

/**
 * Set blogTitle
 *
 * @param string $blogTitle
 *
 * @return Blog
 */
public function setBlogTitle($blogTitle)
{
    $this->blogTitle = $blogTitle;

    return $this;
}

/**
 * Get blogTitle
 *
 * @return string
 */
public function getBlogTitle()
{
    return $this->blogTitle;
}

/**
 * Set blogContent
 *
 * @param string $blogContent
 *
 * @return Blog
 */
public function setBlogContent($blogContent)
{
    $this->blogContent = $blogContent;

    return $this;
}

/**
 * Get blogContent
 *
 * @return string
 */
public function getBlogContent()
{
    return $this->blogContent;
}

/**
 * @return Profile 
 */
public function getprofileId()
{
    return $this->profile_id;
}

/**
 * @param Profile $profile_id
 */
public function setProfileId(Profile $profile _id)
{
    $this->profile_id = $profile_id;
}

}

【问题讨论】:

  • 我认为,您忘记了实体的 @ORM\Entity() 注释。试试this link。这是entity example的要点。
  • 我已经尝试过了,但没有别的...

标签: php symfony orm doctrine-orm


【解决方案1】:

您的实体的注释有误。它们应该是这样的:

//Profile.php
<?php
namespace AppBundle\Entity\Profile
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity()
 */
class Profile {
 /**
  * @var int
  */
 private $id;

 /**
  * @ORM\OneToMany(targetEntity="AppBundle\Entity\Blog", mappedBy="product_id")
  */
 private $blogs;

 ...

}

还有另一个实体:

//博客.php

class Blog { 
 /** * @var int */ 
 private $id;
 ...
 /**
  * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Profile",inversedBy="blogs")
  *  @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
  */
 private $product_id;

 ...
}

【讨论】:

  • 感谢奥斯卡佩雷斯。我已经这样做了,但失败了。问题是 Symfony 3.1 不支持这种格式。此版本支持迁移公式。我已经通过生成新的迁移类取得了成功。我已经用了 up() 方法来完成任务。
  • 请使用正确的解决方案为您的问题添加答案,以便我们都可以将其作为将来的参考。
  • 我大约 8 分钟前回答了这个问题
  • 完美!非常感谢你
【解决方案2】:

首先我在终端中运行这个命令..

$ php bin/console doctrine:migrations:generate

然后我在up() 方法中编写我的代码。下面是我的代码..

class Version20160831074611 extends AbstractMigration
{
    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {

        $blogTable = $schema->getTable('blog');
        $product1Table =$schema->getTable('product1');

        if(!$blogTable->hasColumn('product1_id'))
       {
            $blogTable->addColumn('product1_id','integer')->setNotnull(false);
        }
        if(!$blogTable->hasIndex('FK_product_idx'))
       {
            $blogTable->addIndex(['product1_id'],'FK_product_idx');
        }
        if(!$blogTable->hasForeignKey('FK_product1'))
        {
            $blogTable->addForeignKeyConstraint($product1Table,
                ['product1_id'],
                ['id'],
                ['OnUpdate'=>'CASCADE','OnDelete'=>'CASCADE','FK_product1']
            );
        }

    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your          needs

    }
} 

最后我在终端中运行这个命令并完成...

$ php bin/console doctrine:migrations:migrate 20160831074611

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2013-04-15
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多