我的实体是:
<?php
namespace model\base;
use Doctrine\ORM\Mapping as ORM;
/**
* CategoryRelationship
*/
class CategoryRelationship
{
/**
* @var integer
*/
private $id;
/**
* @var \model\base\Product
*/
private $product;
/**
* @var \model\base\Category
*/
private $category;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product
*
* @param \model\base\Product $product
* @return CategoryRelationship
*/
public function setProduct(\model\base\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \model\base\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set category
*
* @param \model\base\Category $category
* @return CategoryRelationship
*/
public function setCategory(\model\base\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \model\base\Category
*/
public function getCategory()
{
return $this->category;
}
}
表示法是:
<?php
use Doctrine\ORM\Mapping\ClassMetadataInfo;
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->setPrimaryTable(array(
'name' => 'category_relationship',
'indexes' =>
array(
'category_id' =>
array(
'columns' =>
array(
0 => 'category_id',
),
),
'product_id' =>
array(
'columns' =>
array(
0 => 'product_id',
),
),
),
));
$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT);
$metadata->mapField(array(
'fieldName' => 'id',
'columnName' => '_id',
'type' => 'integer',
'nullable' => false,
'unsigned' => false,
'id' => true,
));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_IDENTITY);
$metadata->mapOneToOne(array(
'fieldName' => 'product',
'targetEntity' => 'Product',
'cascade' =>
array(
),
'mappedBy' => NULL,
'inversedBy' => NULL,
'joinColumns' =>
array(
0 =>
array(
'name' => 'product_id',
'referencedColumnName' => 'product_id',
),
),
'orphanRemoval' => false,
));
$metadata->mapOneToOne(array(
'fieldName' => 'category',
'targetEntity' => 'Category',
'cascade' =>
array(
),
'mappedBy' => NULL,
'inversedBy' => NULL,
'joinColumns' =>
array(
0 =>
array(
'name' => 'category_id',
'referencedColumnName' => 'category_id',
),
),
'orphanRemoval' => false,
));
如何按类别检索数据? 基本上我想更新CategoryRelationship但首先我需要通过product_id检索一个对象。