H
H
Hfnas2019-08-03 22:09:26
Doctrine ORM
Hfnas, 2019-08-03 22:09:26

How to fix "Error: Class Blog\Entity\Post has no association named img_id" error?

We will assume that several articles can have one picture. It turns out
Class Blog\Entity\Post

class Post{
 /**@ORM\Column(name="img_id")
     * @ManyToOne(targetEntity="\Blog\Entity\Images")
     * @JoinColumn(name="img_id", referencedColumnName="id")
     */
    protected $img;
}

Class Blog\Entity\Images
class Images
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id")
     * @ORM\GeneratedValue
     */

    protected $id;
}

added repositories
class PostRepository extends EntityRepository
{

    /**
     * Retrieves all published posts whith picture in descending date order.
     * @return Query
     */
    public function findPublishedPostsWithImg()
    {
        $entityManager = $this->getEntityManager();

        $queryBuilder = $entityManager->createQueryBuilder('p');
//без джойна работает.
        $queryBuilder->select('p')
            ->from(Post::class, 'p')
            ->where('p.status = ?1')
            ->leftJoin('p.img','c')
            ->addSelect('c')
            ->orderBy('p.dateCreated', 'DESC')
            ->setParameter('1', Post::STATUS_PUBLISHED);

        return $queryBuilder->getQuery();
    }
}

Next in the controller
public function indexAction()
    {
        $page = $this->params()->fromQuery('page', 1);
        $tagFilter = $this->params()->fromQuery('tag', null);

        if ($tagFilter) {

            // Filter posts by tag
            $query = $this->entityManager->getRepository(Post::class)
                ->findPostsByTag($tagFilter);

        } else {
            // Get recent posts
            $query = $this->entityManager->getRepository(Post::class)
                ->findPublishedPostsWithImg();
        }

        $adapter = new DoctrineAdapter(new ORMPaginator($query, true));
        $paginator = new Paginator($adapter);
        $paginator->setDefaultItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        // Get popular tags.
        $tagCloud = $this->postManager->getTagCloud();

        // Render the view template.
        return new ViewModel([
            'posts' => $paginator,
            'postManager' => $this->postManager,
            'tagCloud' => $tagCloud
        ]);
    }

I don’t know what to fix, I did it by analogy with
this topic , following the example of user-address.
Error producing an iterator

[Semantical Error] line 0, col 56 near 'c WHERE p.status': Error: Class Blog\Entity\Post has no association named img_id

SELECT p, c FROM Blog\Entity\Post p INNER JOIN p.img_id c WHERE p.status = ?1 ORDER BY p.dateCreated DESC
- Seems like a valid request, why doesn't it work?
I don’t know, I thought the docks would help me, I read it, I thought, I described the essence incorrectly, it seems to be true.
Tell me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question