将数组映射到Symfony2 / Doctrine2中的实体 [英] Map array to entity in Symfony2/Doctrine2

查看:87
本文介绍了将数组映射到Symfony2 / Doctrine2中的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DoctrineFixtures包在开发过程中创建示例实体。在我的ORM fixtures load()方法中,我将数据定义为关联数组,并在循环中创建实体对象。

I'm using the DoctrineFixtures bundle to create example entities during development. In my ORM fixtures load() method, I define the data as associative arrays and create the entity object in a loop.

<?php
// ...
public function load($manager) {
    $roleDefs = array(
        'role-1' => array(
             'role' => 'administrator'
        ),
        'role-2' => array(
             'role' => 'user'
        ),
    );

    foreach($roleDefs as $key => $roleDef) {
        $role = new Role();
        $role->setRole($roleDef['role']);
        $manager->persist($role);

        $this->addReference($key, $role);
    }

    $manager->flush();
}

我总是使用相同的数组模式。每个数组元素使用实体的属性名称(以下划线表示)作为索引。如果实体结构变得更加复杂,那么有很多 $ entity-> setMyProperty($ def ['my_property']); 行。

I always use the same array schema. Every array element uses the property name (in underscore notation) of the entity as index. If the entity structure becomes more complex, there are a lot of $entity->setMyProperty($def['my_property']); lines.

我认为将属性名映射到setter方法的问题在Symfony和Doctrine中是一个非常常见的问题,因为在许多情况下可以发现这种类型的映射(例如将表单映射到实体)。

I think the problem of mapping propertynames to setter methods is a very common problem in Symfony and Doctrine as this type of mapping is found in many situations (e.g. mapping forms to entities).

现在我想知道是否有一个可以用于映射的内置方法。要获得一个解决方案,如$ / $
$ b

Now I'm wondering if there is a built-in method that can be used for mapping. It would be nice to have a solution like

foreach($defs as $key => $def) {
   $entity = $magicMapper->getEntity('MyBundle:MyEntity', $def);
   // ...
}

有人知道这可以要实现?

Has someone an idea how this can be achieved?

非常感谢
Hacksteak

Thanks a lot, Hacksteak

推荐答案

p>有时在创建灯具时使用循环。我不知道这个解决方案是否符合您的要求,但是我发现构建灯具的最灵活的方法,并且随着时间的推移快速添加新的属性,如果你需要做的是做以下...假设创建一堆博客帖子:

I sometimes use loops when creating fixtures. I'm not sure if this solution fits your requirements, but I find that the most flexible way to build fixtures and quickly add new properties over time if you need is to do the following... Assuming the creation of a bunch of blog posts:

// an array of blog post fixture values
$posts = array(
    array(
        'title' => 'Foo',
        'text'  => 'lorem'
        'date'  => new \DateTime('2011-12-01'),
    ),
    array(
        'title' => 'Bar',
        'text'  => 'lorem'
        'date'  => new \DateTime('2011-12-02'),
    ),
    // more data...
);

// loop over the posts
foreach ($posts as $post) {
    // new entity
    $post = new Post();

    // now loop over the properties of each post array...
    foreach ($post as $property => $value) {
        // create a setter
        $method = sprintf('set%s', ucwords($property)); // or you can cheat and omit ucwords() because PHP method calls are case insensitive
        // use the method as a variable variable to set your value
        $post->$method($value);
    }

    // persist the entity
    $em->persist($post);
}

这样,您可以通过将新值添加到数组中来添加更多属性。

This way you can add more properties by just adding the new values to your array.

这篇关于将数组映射到Symfony2 / Doctrine2中的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆