php 如何使用 Symfony2 创建实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4983998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:03  来源:igfitidea点击:

How to create entity with Symfony2

phpsymfony

提问by acubens

My general question is how to create entities and repositories with symfony2?

我的一般问题是如何使用 symfony2 创建实体和存储库?

  • How to create entity/repository with a schema.yml with doctrine orm? Where i must save schema.yml file? What are the commands to type in the console?
  • I create a class entity without schema.yml what to do after? Command!?
  • Where i must save my entity/repository file when entity is general for all the project or specific to a bundle?
  • 如何使用带有学说 orm 的 schema.yml 创建实体/存储库?我必须在哪里保存 schema.yml 文件?在控制台中键入的命令是什么?
  • 我创建了一个没有 schema.yml 的类实体之后该怎么办?命令!?
  • 当实体适用于所有项目或特定于捆绑包时,我必须在哪里保存我的实体/存储库文件?

回答by acubens

My solutions (i don't know if it's good, best practice!?)

我的解决方案(我不知道它是否好,最佳实践!?)

YML

YML

Create "Entities.User.dcm.yml" file in HelloBundle/Resources/config/doctrine/metadata/orm with this code (by example):

使用以下代码(示例)在 HelloBundle/Resources/config/doctrine/metadata/orm 中创建“ Entities.User.dcm.yml”文件:

Entities\User:
  type: entity
  table: users
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 50

Then

然后

php app/console doctrine:mapping:import "HelloBundle" yml

php app/console doctrine:generate:entities "HelloBundle"

Then, you can test it in your controller with:

然后,您可以在控制器中使用以下命令对其进行测试:

$user = new \Sensio\HelloBundle\Entity\User;
$user->setName('Acubens');
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($user);
$em->flush();

or with PHP

或使用PHP

Create "User.php" file in HelloBundle\Entity with this code

使用此代码在 HelloBundle\Entity 中创建“User.php”文件

// Sensio/HelloBundle/Entity/User.php
namespace Sensio\HelloBundle\Entity;

/**
 * @orm:Entity
 */
class User
{
    /**
     * @orm:Id
     * @orm:Column(type="integer")
     * @orm:GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @orm:Column(type="string", length="255")
     */
    protected $name;
}

Then

然后

php app/console doctrine:mapping:import "HelloBundle"

this will generate in "HelloBundle/Resources/config/doctrine/metadata/orm"

这将在“HelloBundle/Resources/config/doctrine/metadata/orm”中生成

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Sensio\HelloBundle\Entity\User" table="user">
    <change-tracking-policy>DEFERRED_IMPLICIT</change-tracking-policy>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="name" type="string" column="name" length="255"/>
    <lifecycle-callbacks/>
  </entity>
</doctrine-mapping>

Thendelete User.php

然后删除User.php

php app/console doctrine:generate:entities "HelloBundle"

After you have a new nice file User.php

在你有了一个新的好文件 User.php 之后

<?php

namespace Sensio\HelloBundle\Entity;

/**
 * Sensio\HelloBundle\Entity\User
 */
class User
{
    /**
     * @var string $name
     */
    private $name;

    /**
     * @var integer $id
     */
    private $id;


    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

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

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

And you how do you do? Why i must specify HelloBundle in my commands?

你怎么办?为什么我必须在我的命令中指定 HelloBundle?

ps: my config.yml

ps:我的config.yml

doctrine.dbal:
    driver:   pdo_mysql
    dbname:   sf2
    user:     root
    password: 
    logging:  %kernel.debug%
doctrine.orm:
    auto_generate_proxy_classes: %kernel.debug%
    mappings:
        HelloBundle: ~

回答by wenders

If you're using YML annotation to generate your schema you could have this:

如果您使用 YML 注释来生成您的架构,您可以这样:

Namespace\NameBundle\Entity\Build:
  type: entity
  repositoryClass: Namespace\NameBundle\Entity\Repository\BuildRepository
  ...

when using the command line app/console doctrine:generate:entities NamespaceNameBundle This would generate your entity and repository class automatically

当使用命令行 app/console 指令时:generate:entities NamespaceNameBundle 这将自动生成您的实体和存储库类

回答by Hakan Deryal

http://docs.symfony-reloaded.org/guides/doctrine/orm/index.html

http://docs.symfony-reloaded.org/guides/doctrine/orm/index.html

This section should have all the information you need.

此部分应包含您需要的所有信息。

Although Symfony2 is in stabilization stage now, the latest PR6 release still have lots of features missing and most likely things will change at RC1 version. Dont be afraid to dive into the code and figure things out yourself.

尽管 Symfony2 现在处于稳定阶段,但最新的 PR6 版本仍然缺少许多功能,并且很可能在 RC1 版本中发生变化。不要害怕深入代码并自己解决问题。

回答by Nikola Petkanski

I've been searching for a solution using YML mapping, as it's nowhere to be found within the book. After chiling aroung a bit, I've found that your mappings should follow these rules:

我一直在寻找使用 YML 映射的解决方案,因为在书中找不到它。在冷静下来之后,我发现您的映射应遵循以下规则:

  • the file has to be placed at src/YourAppName/YourBundleName/Resources/config/doctrine/EntityName.orm.yml
  • the entity class name should be full (eg: YourAppName\YourBundleName\Entity\EntityName
  • 该文件必须放在 src/YourAppName/YourBundleName/Resources/config/doctrine/EntityName.orm.yml
  • 实体类名称应该是完整的(例如:YourAppName\YourBundleName\Entity\EntityName

I hope this helps.

我希望这有帮助。

回答by dxb

I think that you must choose between class creation or yml configuration. If you create a entity class and the config yml file, and then execute doctrine:generate:entities You have an error. I recommand to you to stuck with class style with anotations, and then use $ php app/console doctrine:database:create $ php app/console doctrine:schema:create

我认为您必须在类创建或 yml 配置之间进行选择。如果创建实体类和config yml文件,然后执行doctrine:generate:entities就会报错。我建议您坚持使用带有注释的类样式,然后使用 $ php app/console 原则:数据库:创建 $ php app/console 原则:模式:创建