0

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

实体

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

    }
}
4

2 回答 2

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;       
  }
}
于 2013-02-21T20:11:26.313 回答
1

It's usually the case with imports like this that your legacy table doesn't directly map to your new one (in terms of field names, constraints, etc), and may not even be in the same DBMS, so really the best option is a slightly manual approach. Execute your SQL query against your legacy database in your favourite old-fashioned way to get your users as simple arrays, then loop through them and create entities:

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

I notice you're thinking of calling save() on your User objects in your controller, but it doesn't generally work that way in Doctrine as your entities will be plain objects which don't inherit from anything and don't have any special methods. The way to save the entity to your new database is to grab the entity manager and call its persist method.

In your controller:

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

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

As an aside - if you wanted to get a collection of entities by executing a query against your new database that's a slightly different problem to which there's a much more elegant solution. Doctrine Query Language allows you to query your database in a SQL-like way while using the language of your objects. With DQL, the results of your queries will by default be hydrated into Doctrine entites.

于 2013-02-21T17:31:21.173 回答