Php Doctrine2由find方法填充的多个双向属性
我想从现有数据库中创建实体,它是一个N-N关系,用于创建articlesCategories表 我目前有两个实体,文章和类别,我想有很多双向关系 但是,当我尝试使用findByXXX或findOneByXXX方法获取文章或类别时,我的ManyToMany属性为NULL,或者我有一个ORM异常:实体“App\Models\article”没有字段“categories”。因此,您不能在实体的存储库中调用“findByCategories” 数据库: 表物品:物品、名称、说明、价格、尺寸 表类别:idCategory、名称、百分比税 表ArticlesCategory:idArticle,idCategory 实体: 类别Php Doctrine2由find方法填充的多个双向属性,php,doctrine-orm,orm,Php,Doctrine Orm,Orm,我想从现有数据库中创建实体,它是一个N-N关系,用于创建articlesCategories表 我目前有两个实体,文章和类别,我想有很多双向关系 但是,当我尝试使用findByXXX或findOneByXXX方法获取文章或类别时,我的ManyToMany属性为NULL,或者我有一个ORM异常:实体“App\Models\article”没有字段“categories”。因此,您不能在实体的存储库中调用“findByCategories” 数据库: 表物品:物品、名称、说明、价格、尺寸 表类别:i
/**
* @Entity
* @Table(name="category")
*/
class Category
{
/**
* @var integer
*
* @Id
* @Column(name="idCategory", type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @var string
* @Column(name="percentTaxe",type="decimal", precision=10, scale=0, nullable=false)
*/
private $percentTaxe;
/*
* Many categories have many articles
* @ManyToMany(targetEntity="App\Models\Article", inversedBy="categories")
* @JoinTable(name="articlesCategories",
* joinColumns={@JoinColumn(name="idCategory", referencedColumnName="idCategory")},
* inverseJoinColumns={@JoinColumn(name="idArticle", referencedColumnName="idArticle")}
* )
*/
private $articles;
/*Constructor*/
public function __construct(){
$toto = "toto";
var_dump($toto);
$this->articles = new ArrayCollection();
}
/***************************
Getters / Setters
****************************/
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getPercentTaxe(){
return $this->percentTaxe;
}
public function getArticles(){
return $this->articles;
}
/************************/
public function setId($id){
$this->id = $id;
}
public function setName($name){
$this->name = htmlspecialchars($name);
}
public function setPercentTaxe($percentTaxe){
$this->percentTaxe = htmlspecialchars($percentTaxe);
}
public function setArticles(\Doctrine\Common\Collections\Collection $articles)
{
$this->articles = $articles;
}
/***************************
Getters / Setters
****************************/
public function addArticle(App\Models\Article $article)
{
var_dump($article);
$article->addCategory($this); // synchronously updating inverse side
$this->articles[] = $article;
}
文章
/**
* @Entity
* @Table(name="article")
*/
class Article
{
/**
* @var integer
*
* @Id
* @Column(name="idArticle", type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @var string
* @Column(name="description",type="string", nullable=true)
*/
private $description;
/**
* @var string
* @Column(name="priceHT",type="decimal", precision=10, scale=3, nullable=false)
*/
private $priceHT;
/**
* @var string
* @Column(name="size", type="string", length=3, nullable=true)
*/
private $size;
/*
* Many articles have many categories
* @ManyToMany(targetEntity="App\Models\Category", inversedBy="articles")
* @JoinTable(name="articlesCategories",
* joinColumns={@JoinColumn(name="idArticle", referencedColumnName="idArticle")},
* inverseJoinColumns={@JoinColumn(name="idCategory", referencedColumnName="idCategory")}
* )
*/
private $categories;
/*Constructor*/
public function __construct(){
echo"tata";
$this->categories = new ArrayCollection();
echo"tata";
}
/***************************
Getters / Setters
****************************/
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getDescription(){
return $this->description;
}
public function getPriceHT(){
return $this->priceHT;
}
public function getSize(){
return $this->size;
}
public function getCategories(){
return $this->categories;
}
/************************/
public function setId($id){
$this->id = $id;
}
public function setName($name){
$this->name = htmlspecialchars($name);
}
public function setDescription($description){
$this->description = htmlspecialchars($description);
}
public function setPriceHT($priceHT){
$this->priceHT = htmlspecialchars($priceHT);
}
public function setSize($size){
$this->size = htmlspecialchars($size);
}
public function setCategories($categories){
$this->categories = $categories;
}
/***************************
Getters / Setters
****************************/
public function addCategory(App\Models\Category $category)
{
$category->addArticle($this); // synchronously updating inverse side
$this->categories[] = $category;
}
/************************/
public function hydrate($data)
{
foreach($data as $key => $value)
{
// Get back the setter name wich correspond to the attribute
$method = 'set'.ucfirst($key);
// if the good setter exist.
if(methodexists($this, $method))
{
$this->$method($value);
}
}
}
}
经理
当我var_转储我的$category时,我有:class-App\Models\category\122(4){private$id=>int(2)private$name=>string(7)“costs”private$percentTaxe=>string(2)“20”private$articles=>NULL}
我发现我的类别和文章是空的,而不是空数组,因为我不明白为什么它不填充它
我只使用Doctrine2,不使用symfony。首先,您在这两个实体的类别和文章字段上的注释有错误。为了有效,它们应该以/**而不是/*开头。更改:
/*
* Many categories have many articles
到
物品实体的$categories字段也是如此
同时将其中一个字段(项目或类别)更改为关系的反面,例如
inversedBy="categories"
到
mappedBy=“类别”
在问题的另一部分,的默认EntityRepository查找方法(findBy,findOneBy)不支持按多对多关系筛选(至少目前不支持)。您将不得不付出额外的努力并使用createQueryBuilder,例如:
$qb = $doctrine->getRepository(Article::class)->createQueryBuilder('a');
$qb->select(
'a',
'cat'
)
->innerJoin( 'a.categories', 'cat' )
->where('cat.id =1');
最好创建您自己的ArticleRepository和CategoryRepository,并在其中定义查找方法 那真的是斯内基,谢谢你准确地指出如何解决我的问题。我将为高级查询创建一个生成器,但目前我可以不用它来完成我的工作。
inversedBy="categories"
$qb = $doctrine->getRepository(Article::class)->createQueryBuilder('a');
$qb->select(
'a',
'cat'
)
->innerJoin( 'a.categories', 'cat' )
->where('cat.id =1');