当我使用docTine使用oneToOne时,我遇到了从StockCategories Entity和StockTax Entity获取数据的问题。我实现oneToOne的主要实体是StockLevels。问题是我得到了在表stock_categories记录(1,计算机,计算机,1)和(1,电话,电话,1)。当我向stock_levels添加新内容时,我想将它们作为选择类型。在这里你有屏幕(http://i44.tinypic.com/307yrty.jpg)从它和我的文件下面。
// Application\StockBundle\Entity\StockLevels
/**
* @var \Application\StockBundle\Entity\StockTax
*/
private $stocktax;
/**
* Set stocktax
*
* @param \Application\StockBundle\Entity\StockTax $stocktax
* @return StockLevels
*/
public function setStocktax(\Application\StockBundle\Entity\StockTax $stocktax = null)
{
$this->stocktax = $stocktax;
return $this;
}
/**
* Get stocktax
*
* @return \Application\StockBundle\Entity\StockTax
*/
public function getStocktax()
{
return $this->stocktax;
}
/**
* @var \Application\StockBundle\Entity\StockCategories
*/
private $stockcategories;
/**
* Set stockcategories
*
* @param \Application\StockBundle\Entity\StockCategories $stockcategories
* @return StockLevels
*/
public function setStockcategories(\Application\StockBundle\Entity\StockCategories $stockcategories = null)
{
$this->stockcategories = $stockcategories;
return $this;
}
/**
* Get stockcategories
*
* @return \Application\StockBundle\Entity\StockCategories
*/
public function getStockcategories()
{
return $this->stockcategories;
}
------------------------------------------------------------
// Application\StockBundle\Form\Type\StockCategoriesType
namespace Application\StockBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class StockCategoriesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\StockBundle\Entity\StockCategories',
));
}
public function getName()
{
return 'stockcategories';
}
}
--------------------------------------------
// Application\StockBundle\Admin\StockLevelsView
/**
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
*
* @return void
*/
protected function configureFormFields(FormMapper $formMapper)
{
$this->user = $this->securityContext->getToken()->getUser();
$formMapper
->with('General')
->add('sku', null, array('required' => true))
->add('name', null, array('required' => true))
->add('description', null, array('required' => false))
->add('stockcategories' , new StockCategoriesType())
->add('pkwiu' , 'choice', array('choices' => array(1 => 'pkwiu1', 2 => 'pkwiu2')))
->add('type' , 'choice', array('choices' => array(1 => 'Produkt', 2 => 'Usługa')))
->add('status' , 'checkbox', array('required' => false))
->add('lastmodified_date' , 'hidden', array('data' => date('Y-m-d h:i:s')))
->add('lastmodified_user_id' , 'hidden', array('data' => $this->user->getId()))
->end()
->with('Barcode')
->add('barcode', null, array('required' => false))
->end()
->with('Price')
->add('price', 'number', array('required' => true))
->add('price_old', 'number', array('required' => false))
->add('stocktax' , new StockTaxType())
->add('currency' , 'hidden', array('data' => 'PLN'))
->end()
->with('Warehouse')
->add('quantity', 'number', array('required' => true))
->add('reorder_level', 'number', array('required' => false))
->end()
->with('Extra')
->add('unit', 'choice', array('choices' => array(1 => 'szt', 2 => 'waga')))
->add('weight', 'number', array('required' => false))
->end()
;
if(!$this->getSubject()->getId()) $formMapper->with('General')
->add('created_date', 'hidden', array('data' => date('Y-m-d H:i:s')))
->add('created_user_id' , 'hidden', array('data' => $this->user->getId()));
}
在StockLevels.orm.yml中我得到了:
oneToOne:
stocktax:
targetEntity: StockTax
joinColumn:
name: tax_id
referencedColumnName: id
stockcategories:
targetEntity: StockCategories
joinColumn:
name: category_id
referencedColumnName: id
非常感谢您的帮助。
答案 0 :(得分:0)
我认为来自帖子评论的“Adam-E”是正确的,但是,如果要建立双向关系,则需要定义owning
和inverse
方。
另外,我对你的实体结构有点不清楚,所以请告诉我是否犯了一些错误。重要的部分是定义mappedBy
和inversedBy
,如官方文档中所述:Bidirectional OneToOne
<强> StockLevels.orm.yml:强>
oneToOne:
stocktax:
targetEntity: StockTax
mappedBy: stockcategories
joinColumn:
tax_id:
referencedColumnName: id
<强> StockTax.orm.yml 强>
oneToOne:
stockcategories:
targetEntity: StockLevels
inversedBy: stocktax