在ZF2中使用Doctrine的一对多关系

时间:2013-02-20 13:59:42

标签: php doctrine zend-framework2

这是我的代码

类别文件

 <?php

namespace Category\Entity;

 use Doctrine\ORM\Mapping as ORM,
Zend\Form\Annotation;

/**
 * A Category entity.
 *
   * @ORM\Entity
 * @ORM\Table(name="categories")
 * @ORM\Table(name="categories"))
 * 
 * @property int $id
 * @property string $category_name
 * @property string $status
  * @property date $created_at
 * @property date $created_at

  * 
  * @Annotation\Name("Category")
  */
 class Category {

/**
 * @ORM\Id
 * @ORM\Column(type="integer");
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 * @Annotation\Required(false)
 */
protected $id;

/**
 * @ORM\Column(type="string")
 * 
 * @Annotation\Attributes({"type":"text"})
 * @Annotation\Options({"label":"category_name:"})
 * @Annotation\Filter({"name":"StringTrim"})
 * @Annotation\Filter({"name":"StripTags"})
 */
protected $category_name;

/**
 *  @ORM\Column(type="integer");
 * @Annotation\Options({"label":"Status:"})
 * 
 * @Annotation\Required(false)
 */
protected $status;

/**
 * @ORM\Column(type="datetime")
 * 
 * @Annotation\Required(false)
 */
protected $created_at;

/**
 * @ORM\Column(type="datetime")
 * 
 * @Annotation\Required(false)
 */
protected $updated_at;

/**
 * @ORM\OneToMany(targetEntity="\Product\Entity\Product" , mappedBy="category")
 * 
 * 
 */
protected $category;

public function __get($property) {
    return $this->$property;
}

/**
 * 
 * 
 * Magic setter to save protected properties.
 *
 * @param string $property
 * @param mixed $value
 */
   public function __set($property, $value) {
    $this->$property = $value;
  }

   public function getArrayCopy() {
    return array(
        'category_name' => $this->category_name,
        'status' => $this->status,
    );
  }

public function exchangeArray($data) {
    $this->id = (isset($data['id'])) ? $data['id'] : null;
    $this->category_name = (isset($data['category_name'])) ? $data['category_name'] : null;
    $this->status = (isset($data['status'])) ? $data['status'] : 0;
}



public function populate($data) {
    $this->id = isset($data['id']) ? $data['id'] : $this->id;
    $this->category_name = isset($data['category']) ? $data['category'] : $this->category_name;
    $this->status = isset($data['status']) ? $data['status'] : $this->status;
}


}

这是我的CategoriesNames文件

  <?php

  namespace Admin\Entity;

 use Doctrine\ORM\Mapping as ORM,
 Zend\Form\Annotation;

 /**
 * A Category entity.
 *
 * @ORM\Entity
 * @ORM\Table(name="categories_names")
 * @ORM\Table(name="categories_names"))
  * 
 * @property int $id
 * @property string $category
 * @property string $language

 * 
 * @Annotation\Name("language")
 */
 class CategoriesNames {

/**
 * @ORM\Id
 * @ORM\Column(type="integer");
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 * @Annotation\Required(false)
 */
protected $id;

/**
 * 
 * @ORM\ManyToOne(targetEntity="\Category\Entity\Category", inversedBy="category")     
 */
private $category;

/**
 * 
 * @ORM\ManyToOne(targetEntity="Languages", inversedBy="language")     
 */
private $language;

public function __get($property) {
    return $this->$property;
}

/**
 * 
 * 
 * Magic setter to save protected properties.
 *
 * @param string $property
 * @param mixed $value
 */
public function __set($property, $value) {
    $this->$property = $value;
}

public function getArrayCopy() {
    return array(
        'category_name' => $this->category_name,
        'status' => $this->status,
    );
}

public function exchangeArray($data) {
    $this->id = (isset($data['id'])) ? $data['id'] : null;
    $this->category_name = (isset($data['category'])) ? $data['category'] : null;
    $this->status = (isset($data['language'])) ? $data['language'] : 0;
}



public function populate($data) {
    $this->id = isset($data['id']) ? $data['id'] : $this->id;
    $this->category_name = isset($data['category']) ? $data['category'] :   $this->category;
    $this->status = isset($data['language']) ? $data['language'] : $this->language;
}

}

这是我的语言实体

 <?php

 namespace Admin\Entity;

 use Doctrine\ORM\Mapping as ORM,
   Zend\Form\Annotation;

/**
* A Category entity.
*
* @ORM\Entity
* @ORM\Table(name="languages")
* @ORM\Table(name="languages"))
 * 
*  @property int $id
* @property string $language_name
* @property string $language_code

* 
 * @Annotation\Name("language")
*/
class Languages {

/**
 * @ORM\Id
 * @ORM\Column(type="integer");
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 * @Annotation\Required(false)
 */
protected $id;

/**
 * @ORM\Column(type="string")
 * 
 * @Annotation\Attributes({"type":"text"})
 * @Annotation\Options({"label":"language_name:"})
 * @Annotation\Filter({"name":"StringTrim"})
 * @Annotation\Filter({"name":"StripTags"})
 */
protected $language_name;

/**
 * @ORM\Column(type="string")
 * 
 * @Annotation\Attributes({"type":"text"})
 * @Annotation\Options({"label":"language_code:"})
 * @Annotation\Filter({"name":"StringTrim"})
 * @Annotation\Filter({"name":"StripTags"})
 */
protected $language_code;

/**
 * @ORM\OneToMany(targetEntity="\Admin\Entity\CategoriesNames" , mappedBy="language")
 * 
 * 
 */
protected $language;

public function __get($property) {
    return $this->$property;
}

/**
 * 
 * 
 * Magic setter to save protected properties.
 *
 * @param string $property
 * @param mixed $value
 */
public function __set($property, $value) {
    $this->$property = $value;
}

public function getArrayCopy() {
    return array(
        'category_name' => $this->category_name,
        'status' => $this->status,
    );
}

public function exchangeArray($data) {
    $this->id = (isset($data['id'])) ? $data['id'] : null;
    $this->category_name = (isset($data['category_name'])) ? $data['category_name'] : null;
    $this->status = (isset($data['status'])) ? $data['status'] : 0;
}



public function populate($data) {
    $this->id = isset($data['id']) ? $data['id'] : $this->id;
    $this->category_name = isset($data['category']) ? $data['category'] : $this->category_name;
    $this->status = isset($data['status']) ? $data['status'] : $this->status;
}

现在我得到的错误是

  

目标实体Admin \ Entity \ CategoriesNames在'Admin \ Entity \ Languages#language'中找不到。

有例外

  

/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:428

编辑:我也发布了语言文件

0 个答案:

没有答案