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