symfony2从查询创建doctrine集合

时间:2013-02-21 15:11:19

标签: symfony orm import doctrine-orm entity

不确定这是否可行,但我希望从查询中创建一个学说集合。我的想法是使用一些预设值来填充集合,这样我就可以更新数据库,将其视为从旧系统导入/生成用户到新系统。我正在努力与存储库位。

实体

// Portal\UserBundle\Entity\User.php

namespace Portal\UserBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;


/** 
 * @ORM\Entity
 */
class User
{
    /** 
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $id;

    /** 
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $fistname;

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

    // etc...
}

存储库

namespace Portal\UserBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
   public function getGenerateNewUsers()
   {
    // acts as an import from an old user table
    $sql = " SELECT firstname, surname, other FROM old_user_table ";

    $userCollection = .... not sure how I link query? 
    return $userCollection;       

   }
}

在控制器内调用

通过上述内容,我打算能够获取新生成的用户循环,并可以访问我的实体方法对象等。

class SetupController extends Controller
{
    public function indexAction(){

        $repository = this->getDoctrine()->getRepository('UserBundle:User');

        $newUsers =   $repository->getGenerateUsers();

        // I can now have access to the users with something like

        foreach($newUsers as $user){
          $user->setFirstName('testing');
          $user->save();
        }

    }
}

2 个答案:

答案 0 :(得分:1)

这样的导入通常就是你的遗留表没有直接映射到你的新表(在字段名称,约束等方面),甚至可能不在同一个DBMS中,所以真的是最好的选项是一种略微手动的方法。以您最喜欢的老式方式对您的旧数据库执行SQL查询,以使您的用户成为简单数组,然后遍历它们并创建实体:

//Set up an empty collection
$collection = new ArrayCollection();

/*Assuming PDO where you have set up and executed a PDO statement $pdoStatement,
  but mysql_fetch_assoc or whatever is appropriate for your DBMS would work equally
  well. $oldUser should just be a plain associative array*/

while($oldUser = $pdoStatement->fetch(PDO::FETCH_ASSOC)){ 
    //Initialise an empty User entity
    $newUser = new User();

    //Set the properties on the entity using the values from your array
    $newUser->setFirstName($oldUser['firstname']);
    //etc

    //Add your user to the collection
    $collection->add($newUser);
}

return $collection

我注意到你正在考虑在控制器中的save()对象上调用User,但它通常不会在Doctrine中那样工作,因为你的实体将是普通对象,而不是继承任何东西,没有任何特殊方法。将实体保存到新数据库的方法是抓取entity manager并调用其persist方法。

在您的控制器中:

$entityManager = $this->get('Doctrine')->getManager();
foreach($users as $user){
    //Manipulate the user here if you need to

    //Then persist it
    $entityManager->persist($user);
}

顺便说一句 - 如果您想通过对数据库执行查询来获取实体集合,这是一个稍微不同的问题,那就是更优雅的解决方案。 Doctrine Query Language允许您在使用对象语言时以类似SQL的方式查询数据库。使用DQL,您的查询结果默认为hydrated到Doctrine entites。

答案 1 :(得分:1)

霍根提到了DQL。这是什么样子,但你必须确保你的旧数据库已经连接好了。结果是一组实体,您可以使用方法调用来存储部分或全部数据。

namespace Portal\UserBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
   public function getGenerateNewUsers()
   {
    $qb = $this->getEntityManager()
               ->getRepository('Bundle:Old_User')->createQueryBuilder('o');
    $query = $qb->getQuery();
    $results = $query->getResult();

    return $results;       
  }
}
相关问题