Answer the question
In order to leave comments, you need to log in
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 Images
{
/**
* @ORM\Id
* @ORM\Column(name="id")
* @ORM\GeneratedValue
*/
protected $id;
}
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();
}
}
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
]);
}
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?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question