如何在yaml文件中使用DoctrineMigrationsBundle?

时间:2018-07-12 09:23:54

标签: php symfony doctrine doctrine-migrations

当我使用上面的示例中的“教义注释”文件时,命令php bin/console doctrine:migrations:diff可以正常工作。

#src/Entity/User.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity(repositoryClass="Repository\UserRepository")
 * @ORM\Table(
 *     schema="data",
 *     name="ts_user",
 *     options={"comment":"Utilisateurs de l'application"},
 *     uniqueConstraints={@ORM\UniqueConstraint(name="uk_usr_email", columns={"usr_email"})}
 * )
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="usr_id")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=true, name="usr_name")
     */
    private $name;

    /**
     * @ORM\Column(type="text", nullable=false, name="usr_email")
     */
    private $email;

    /**
     * @ORM\Column(type="text", nullable=true, name="usr_password")
     */
    private $password;
}

由于某些原因(特征,接口),我必须使用doctrine yaml文件声明我的实体。一旦我用yaml文件替换了注释,迁移命令就会抛出此错误:

  

在OrmSchemaProvider.php第41行中:

     

没有要处理的映射信息

这是我的Yaml文件:

#src/Resources/config/doctrine/User.orm.yml
App\Entity\User:
  type: entity
  schema: data
  table: ts_user
  repositoryClass: Repository\UserRepository
  id:
    id:
      type: integer
      column: usr_id
      generator:
        strategy: AUTO
  fields:
    name:
      type: text
      nullable: true
      column: usr_name
    email:
      type: text
      nullable: false
      column: usr_email
    password:
      type: text
      nullable: true
      column: usr_password
  uniqueConstraints:
    uk_usr_email:
      columns: [usr_email]
  options:
    comment: Utilisateurs de l'application

由于此错误,我认为它没有找到我的yaml文件。是在错误的目录中吗?这个文件放在哪里?我在源代码中看不到任何声明yaml文件目录的信息。我已经在此目录中尝试过但没有成功:

  • src / Entity
  • src / Resources / config / doctrine /
  • src / Resources / config / doctrine / App
  • src / Resources / config / doctrine / App / Entity
  • src / Resources / config / doctrine / Entity

我在做什么错了?

Env:PHP 7.2.3,Symfony 4.1.1(内核:src,env:dev,debug:true)

1 个答案:

答案 0 :(得分:2)

如果自动映射与Doctrine一起使用,则必须检查orm配置并将其设置为使用yml加载那些实体

类似:

[HttpPost]
[Route("api/forge/oss/objects")]
public async Task<dynamic> UploadObject()
{
  // basic input validation
  HttpRequest req = HttpContext.Current.Request;
  if (string.IsNullOrWhiteSpace(req.Params["bucketKey"]))
    throw new System.Exception("BucketKey parameter was not provided.");

  if (req.Files.Count != 1)
    throw new System.Exception("Missing file to upload");

  string bucketKey = req.Params["bucketKey"];
  HttpPostedFile file = req.Files[0];

  // save the file on the server
  var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), 
  file.FileName);
  file.SaveAs(fileSavePath);

  // get the bucket...
  dynamic oauth = await OAuthController.GetInternalAsync();
  ObjectsApi objects = new ObjectsApi();
  objects.Configuration.AccessToken = oauth.access_token;

  // upload the file/object, which will create a new object
  dynamic uploadedObj;
  using (StreamReader streamReader = new StreamReader(fileSavePath))
  {
    uploadedObj = await objects.UploadObjectAsync(bucketKey,file.FileName, 
  (int)streamReader.BaseStream.Length, streamReader.BaseStream,"application/octet- 
   stream");
  }

  // cleanup
  File.Delete(fileSavePath);

  return uploadedObj;
}

有关更多信息,您可以在Symfony的教程中查看Doctrine配置的自定义映射部分

https://symfony.com/doc/3.4/reference/configuration/doctrine.html