如果我定义了以下类,
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;
}
Run Code Online (Sandbox Code Playgroud)
我不应该通过以下方式获得与此类别相关的所有标签:
$categoryTags = $category->getTags();
Run Code Online (Sandbox Code Playgroud)
上面赋值后$ categoryTags中的对象属于Doctrine\ORM\PersistentCollection类型,而我希望它是一个数组.
我使用sql命令手动在category_tag表中添加了关联值,但我可以看到它们是有效的.
我的Tags类看起来像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
Doctrine不返回关联实体集合的简单数组.相反,它返回的实现Doctrine\Common\Collections\Collection.
正如你所数组作为他们扩展您可以使用这些Countable,IteratorAggregate和ArrayAccess接口.
如果你真的需要一个数组(我想不出原因),你可以使用这个toArray()方法.
请阅读文档以了解为什么Doctrine不使用简单数组
| 归档时间: |
|
| 查看次数: |
3684 次 |
| 最近记录: |