If I have the following class defined,
class Category {
/**
*
* @var integer $id
* @Column(name="id", type="integer",nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
*
* @ManyToMany(targetEntity="Tag")
* @JoinColumn(onDelete="SET NULL")
*/
protected $tags;
}
Shouldn’t I be able to get all the tags associated to this category by:
$categoryTags = $category->getTags();
The object in $categoryTags after the above assignment is of the type Doctrine\ORM\PersistentCollection while I expect it to be an array.
I added the association values in category_tag table manually with sql commands, but I can see that they are valid.
My Tags class looks like this:
class Tag extends Tag{
/**
*
* @var integer $id
* @Column(name="id", type="integer",nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @Column(type="string",length=60,nullable=false)
* @var string
*/
protected $tag;
}
Doctrine does not return simple arrays for associated entity collections. Rather, it returns implementations of
Doctrine\Common\Collections\Collection.You can use these as you would an array as they extend the
Countable,IteratorAggregateandArrayAccessinterfaces.If you really need an array (and I can’t think of a reason why), you can use the
toArray()method.Please read the documentation to understand why Doctrine does not use simple arrays