15

My general question is how to create entities and repositories with 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?
8
  • 2
    If you want to persist with Symfony2, the documentation at docs.symfony-reloaded.org should be enough to get you started, including persisting objects to the database and folder structure. If you're having trouble with this, then Dan's answer is the way forward. Tried & tested :-) Commented Feb 13, 2011 at 12:34
  • 2
    Thank for your comments! I use symfony1.4 all the days...I just want to try and understand this new version (stable will come very soon) but i find switching to sf2 is little bit destabilizing. I'm sure i'm not the only! I continue my tests... Commented Feb 14, 2011 at 14:56
  • @acubens Symfony2 is not an upgrade per se for Symfony 1.x - it's been completely rewritten from the ground up, and there will be no upgrade path. So if you're still thinking of switching, bear in mind that it is a completely different framework and a large proportion of what you've done in sf1.4 etc will not be the same in Symfony2. Commented Feb 21, 2011 at 8:42
  • I know that many things are new in symfony2. I don't want migrate my old project sf1 in Sf2... I just want to understand why the commande "php app/console doctrine:generate:entities" don't work (i create a class in an Entitiy folder, active mappings in config.yml)? I don't found good documentation about entities/repositories/proxies Thank Commented Feb 22, 2011 at 9:53
  • Good for you if you're getting in over your head. That's the best way to learn, in my opinion. I'll be very appreciative of symfony2 questions and answers on SO as I dive into symfony2 myself. Commented Mar 24, 2011 at 17:47

5 Answers 5

15

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):

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

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

// 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"

<?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

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

After you have a new nice file 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?

ps: my 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: ~
Sign up to request clarification or add additional context in comments.

3 Comments

For me it says : [InvalidArgumentException] The parameter "doctrine.orm.default_entity_manager" must be defined.
now you can also put all the mapping in mapping.orm.yml in HelloBundle/Resources/config/doctrine
I found the following link useful: symfony.com/doc/current/cookbook/doctrine/… When I tried to follow the steps within the post I encountered few problems.
2

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

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

Comments

1

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.

Comments

1

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:

  • 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

I hope this helps.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.