0

当我使用学说使用 oneToOne 时,我从 StockCategories 实体和 StockTax 实体获取数据时遇到问题。我实现 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

非常感谢您的帮助。

4

1 回答 1

0

我认为帖子评论中的“Adam-E”是正确的,但是,如果为了建立双向关系,您需要定义owninginverse侧。

另外,我对您的实体结构有点不清楚,所以请告诉我是否犯了一些错误。重要的部分是定义两者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
于 2013-07-28T20:32:56.587 回答