How to access repository methods for an entity in symfony2?

前端 未结 4 597
青春惊慌失措
青春惊慌失措 2020-12-15 22:10

I am stuck with a problem please help me with it. Here is the scenarario:

I have an entity \"User\" and corresponding repository \"UserRepository\", inside my entit

4条回答
  •  一个人的身影
    2020-12-15 23:15

    If the bundle is Acme/DemoBundle, then one would expect at a minimum

    User entity

    namespace Acme/DemoBundle/Entity
    
    use Doctrine\ORM\Mapping as ORM;
    
    
    /**
     *
     * @ORM\Table(name="user")
     * @ORM\Entity(repositoryClass="Acme/DemoBundle/Entity/UserRepository")
     */
    class User 
    {
    ...
    }
    

    User repository

    namespace Acme/DemoBundle/Entity
    
    use Doctrine\ORM\Mapping as ORM;
    
    class UserRepository extends EntityRepository
    {
    ...
    }   
    

    It is also true that with an array of ids, one can also do the following in a controller:

    ...
    $em = $this->getDoctrine()->getManager();
    $users = $em->getRepository("AcmeDemoBundle:User")->findAllById($idArray);
    ...
    

    To iterate thru users in a controller, one can then use a foreach loop as in:

    foreach ($users as $user) {
    //each user is an array
    ...
    $id = $user['id'];
    ...
    }
    

    or in a template:

    {% for user in users %}
    ...
    {{ user.firstName }}
    ...
    {% endfor %}
    

提交回复
热议问题