This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Converting Doctrine Relational Annotations to YML Configurat

About symfony tutorials, symfony book and API documentation.

Converting Doctrine Relational Annotations to YML Configurat

by oddz » Sun Jul 03, 2011 9:45 pm

Progressing through the Symfony 2 documentation I have hit a snag. In most sections where yml, annotations/php or xml can be used the alternatives are provided. In most cases these are interchangeable. However, in regards to Doctrine only one can be used per bundle, I believe. I have choosen to use yml. However, the below information in the docs only shows how to get relational mappings hooked up using annotations which will not work because it one or the other and I am using yml. I would appreciate it if someone could tell mm how to convert the annotations below to doctrine yml.

Code: Select all
// src/Acme/StoreBundle/Entity/Category.php
// ...
use Doctrine\Common\Collections\ArrayCollection;

class Category
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
}


Code: Select all
// src/Acme/StoreBundle/Entity/Product.php
// ...

class Product
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;
}


This section is the part this is all outlined in the Symfony 2 docs:

Symfony - Databases and Doctrine ("The Model")

This is my current yml configuration for the Product and Category entity.

Category.orm.yml
Code: Select all
Acme\StoreBundle\Entity\Category:
  type: entity
  table: category
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: '255'
  lifecycleCallbacks: {  }


Product.orm.yml
Code: Select all
Acme\StoreBundle\Entity\Product:
    type: entity
    repositoryClass: Acme\StoreBundle\Repository\ProductRepository
    table: product
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        price:
            type: decimal
            scale: 2
        description:
            type: text


thanks
oddz
Junior Member
 
Posts: 17
Joined: Sun Jul 03, 2011 9:42 pm

Re: Converting Doctrine Relational Annotations to YML Config

by oddz » Tue Jul 05, 2011 6:40 am

SOLVED

It took some digging around in doctrine docs but I was able to figure it out.

Product.orm.yml
Code: Select all
Acme\StoreBundle\Entity\Product:
    type: entity
    repositoryClass: Acme\StoreBundle\Repository\ProductRepository
    table: product
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        price:
            type: decimal
            scale: 2
        description:
            type: text
    manyToOne:
        category:
            targetEntity: Category
            mappedBy: product


Category.orm.yml
Code: Select all
Acme\StoreBundle\Entity\Category:
  type: entity
  table: category
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: '255'
  lifecycleCallbacks: {  }
  oneToMany:
    products:
      targetEntity: Product
      mappedBy: category
oddz
Junior Member
 
Posts: 17
Joined: Sun Jul 03, 2011 9:42 pm

Re: Converting Doctrine Relational Annotations to YML Config

by Timur » Fri Aug 19, 2011 4:22 am

So how did you manage to solve it?

Timur
Timur
Junior Member
 
Posts: 27
Joined: Wed Aug 17, 2011 1:19 pm

Re: Converting Doctrine Relational Annotations to YML Config

by aron11 » Tue Oct 21, 2014 10:34 am

Thanks for share tour experience...
harry
aron11
Junior Member
 
Posts: 1
Joined: Tue Oct 21, 2014 10:32 am