【问题标题】:symfony2 create doctrine collection from querysymfony2 从查询创建学说集合
【发布时间】:2013-02-21 15:11:19
【问题描述】:

不确定这是否可能,但我希望从查询中创建一个学说集合。这个想法是用一些预设值填充集合,这样我就可以更新数据库,就像将用户从旧系统导入/生成到新系统一样。我正在为存储库位而苦苦挣扎。

实体

// 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();
        }

    }
}

【问题讨论】:

标签: symfony orm import doctrine-orm entity


【解决方案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

我注意到您正在考虑在您的控制器中的 User 对象上调用 save(),但在 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);
}

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

【讨论】:

    【解决方案2】:

    霍根提到了 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;       
      }
    }
    

    【讨论】:

    • 这看起来不错。什么是 Bundle:Old_User 是我必须创建的东西 - 我需要创建一个包和一个实体吗?可以跳过吗?
    • 您应该将 old_user 作为与当前用户捆绑在一起的实体。
    猜你喜欢
    • 2011-03-10
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2016-12-04
    • 2014-12-20
    相关资源
    最近更新 更多