keram
keram

Reputation: 2421

Error: Class ...\Entity\.. has no association named

This question is related to my another question: Doctrine2 gives me only first instance of related objects

I came up with bidirectional association to try solve my old issue but now I have another problem.

Schema [EDITED]:

  XYZ\Bundle\CitiesBundle\Entity\Cities:
  type: entity
  table: cities
  fields:
    id:
      id: true
      type: integer
      unsigned: false
      nullable: false
      generator:
        strategy: IDENTITY
    name:
      type: string
      length: 50
      fixed: false
      nullable: false
    landarea:
      type: decimal
      nullable: false
    density:
      type: integer
      unsigned: false
      nullable: false
    population:
      type: integer
      unsigned: false
      nullable: false
  manyToOne:
    state:
      targetEntity: States
      cascade: {  }
      inversedBy: cities
      joinColumn:
        state_id:
          referencedColumnName: id
  lifecycleCallbacks: {  }

Entities:

class Cities
{
    //other vars

    /**
     * @var XYZ\Bundle\CitiesBundle\Entity\States
     */
    private $state;

    //other get/set    

    /**
     * Set state
     *
     * @param XYZ\Bundle\CitiesBundle\Entity\States $state
     */
    public function setState(\XYZ\Bundle\CitiesBundle\Entity\States $state)
    {
        $this->state = $state;
    }

    /**
     * Get state
     *
     * @return XYZ\Bundle\CitiesBundle\Entity\States 
     */
    public function getState()
    {
        return $this->state;
    }
}

class States
{
    //other vars and get/set

    private $cities;

    public function __construct()
    {
        $this->cities = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add cities
     *
     * @param XYZ\Bundle\CitiesBundle\Entity\Cities $cities
     */
    public function addCities(\XYZ\Bundle\CitiesBundle\Entity\Cities $cities)
    {
        $this->cities[] = $cities;
    }

    /**
     * Get cities
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getCities()
    {
        return $this->cities;
    }
}

QueryBuilder usage:

    $query = $em->createQueryBuilder()
                ->select('c','s')
                ->from('CitiesBundle:Cities', 'c')
                ->innerJoin('c.state', 's')
                ->orderBy('c.population', 'DESC')
                ->setMaxResults(10)
                ->getQuery();  

and an error is:

[Semantical Error] line 0, col 58 near 's ORDER BY c.population': Error: Class XYZ\Bundle\CitiesBundle\Entity\Cities has no association named state

generated DQL:

SELECT c, s FROM CitiesBundle:Cities c INNER JOIN c.state s ORDER BY c.population DESC

shame :(

Could someone help me solve my issue?

[EDIT]

I edited cities schema, now error is:

Notice: Undefined index: name in /home/marek/devel/sf2/cities/vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php line 473

Upvotes: 4

Views: 5317

Answers (2)

Onshop
Onshop

Reputation: 3075

Note that when using Symfony, it will look at mapping YAML files first and ignore your entity annotations. I spent ages changing my annotations whilst all the time it was loading the YAML mapping file.

Upvotes: 2

Jovan Perovic
Jovan Perovic

Reputation: 20193

I'm sure joinColumns: (notice the plural) is only used in Many-To-Many relation. In other relations you should use joinColumn.

In Cities XML, try putting:

joinColumn:
    name: state_id
    referencedColumnName: id

other than that try renaming 'ManyToOne' to 'manyToOne' in your XML but I doubt that case is the issue here...

Hope this helps...

Upvotes: 2

Related Questions