简体   繁体   English

如何使用Symfony2创建实体

[英]How to create entity with Symfony2

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

My solutions (i don't know if it's good, best practice!?) 我的解决方案(我不知道它是否好,最佳实践!?)

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>

Then delete 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: ~

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 doctrine时:generate:entities NamespaceNameBundle这会自动生成你的实体和存储库类

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. 尽管Symfony2现在处于稳定阶段,但最新的PR6版本仍然缺少许多功能,而且很可能在RC1版本中会发生变化。 Dont be afraid to dive into the code and figure things out yourself. 不要害怕潜入代码并自己解决问题。

I've been searching for a solution using YML mapping, as it's nowhere to be found within the book. 我一直在寻找使用YML映射的解决方案,因为它在本书中找不到。 After chiling aroung a bit, I've found that your mappings should follow these rules: 经过一段时间的冷却后,我发现你的映射应遵循以下规则:

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

I hope this helps. 我希望这有帮助。

I think that you must choose between class creation or yml configuration. 我认为您必须在类创建或yml配置之间进行选择。 If you create a entity class and the config yml file, and then execute doctrine:generate:entities You have an error. 如果您创建实体类和config yml文件,然后执行doctrine:generate:entities您有错误。 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 我建议你坚持使用anotations的类风格,然后使用$ php app / console doctrine:database:create $ php app / console doctrine:schema:create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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