S
S
Stanislav Shendakov2021-02-25 06:05:51
Doctrine ORM
Stanislav Shendakov, 2021-02-25 06:05:51

How to get all records from Entity in Doctrine?

Good afternoon.
Self-written framework. Doctrine is used, with which I am still superficially familiar. Repositories for tables (for example, automatically, as in Symfony) are not defined separately. There are two classes for polling:

namespace Entities;

use \RN\registry,
    Doctrine\Common\Collections\ArrayCollection;

/**
 * @Table(name="rn_interview_tickets")
 */
class interview extends base_entity {
    /** @Id @Column(type="integer") @GeneratedValue()*/
    protected $id = null;
...
    /** типы опросов
     * @OneToOne(targetEntity="Entities\interviewTypes", inversedBy="interview")
     * @JoinColumn(name="id_ticket_type", referencedColumnName="id")
     */
    private $types;
...

and
namespace Entities;

/**
 * @Entity
 * @Table(name="rn_interview_ticket_types")
 */
class interviewTypes extends base_entity {
    /** @Id @Column(type="integer") @GeneratedValue()*/
    protected $id = null;

    ...

    /**
     * @OneToOne(targetEntity="Entities\interview", mappedBy="types")
     */
    private $interview;

    public function getTypesList() {
...


On the poll editing page, you need to display in SELECT all records from the table (all types of polls). I wanted to do this in the getTypesList method of the interviewTypes class, but it doesn't work. getDoctrine returns 0 in both classes, so I can't reach the required repository. Or do I need to get the EntityManager? Tell me ch.y.d.n.t and how to get it ideologically correct in general. Thank you.

PS I found a solution, I did it in the code outside the Entities classes like this:
...
$o = $this->getObject(); // объект сущности опроса  Entities/interview.php
$em = Registry::getInstance() -> getEntityManager();
$types = $em -> getRepository( get_class( $o -> getTypes() ) ) -> findAll();
...

But the question remained.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flying, 2021-02-25
@Flying

You have incorrect mappings.
I was not very good at understanding what you wanted to depict here... It follows from your classes that interviews can be of many types at the same time, and not vice versa?
But, no matter how it should be - in fact you should still have a 1:N relationship, not 1:1, this is where the main mistake is.
Change the association type, then from the OneToManyoutside you will have a collection of entities, from where you can get them.
Aside note: read at your leisure PSR-1 , PSR-2 , PSR-12

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question