Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine - ORM - Fetching all data using entityManager with associaction

I have two entities user1 and skills, where one user will have many skills.

/** @Entity **/
class user1 {
    /** @Id @Column(type="integer") @GeneratedValue **/
    public $id;
    /** @Column(type="string") **/
    public $name;
    /**
     * 
     * @OneToMany(targetEntity="skills", mappedBy="uid")
     */
    public $skillset;
}

/** @Entity **/
class skills {        
    /** @Id @Column(type="integer") @GeneratedValue **/
    public $id;
    /** @Column(type="string") **/
    public $skill;
    /**
     * @ManyToOne(targetEntity="user1", inversedBy="skillset")
     **/
    public $uid;
}

But I want to fetch all the records using entityManager and getrepository like

$usr= $entityManager->getRepository("user1")->findAll();  

But it gives me data from table 'user1' only. And I am not able to fetch the data from 'skills' which is associated to particular user.

'uid' is the foreign key in 'skills' table of 'id' in 'user1' table.

like image 447
Mahesh Talpade Avatar asked Jul 21 '26 17:07

Mahesh Talpade


1 Answers

I think you may prefer a ManyToMany relationship, because many users will have many skills.

Then you don't have to refer fields as IDs - and ORM's purpose is to hide the relational structure under the object relationship: your $uid field should be called $user.

Such said, your syntax is correct - are you sure your database schema is in sync with the entities? Have you got proper data in it? If you var_dump a single user, what you get?

like image 166
moonwave99 Avatar answered Jul 24 '26 06:07

moonwave99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!