Symfony2 Doctrine2 Relations m:n 为什么在创建关系 bt 时,我的表单计数器不会改变



你好,这是我的问题:我有两个实体:申请实体:

/**
 * @ORMManyToMany(targetEntity="Category", inversedBy="applications")
 */
private $categories;
/**
 * Add categories
 *
 * @param ApplicationsBundleEntityCategory $categories
 * @return Application
 */
public function addCategory(ApplicationsBundleEntityCategory $categories)
{
    $this->categories[] = $categories;
    $categories->addApplication($this);
    return $this;
}
/**
 * Remove categories
 *
 * @param ApplicationsBundleEntityCategory $categories
 */
public function removeCategory(ApplicationsBundleEntityCategory $categories)
{
    $this->categories->removeElement($categories);
}

和类别实体:

/**
 * @var integer
 *
 * @ORMColumn(name="appsCount", type="integer")
 */
private $appsCount;
/**
 * @ORMManyToMany(targetEntity="Application", mappedBy="categories")
 */
private $applications;

/**
 * Add applications
 *
 * @param ApplicationsBundleEntityApplication $applications
 * @return Category
 */
public function addApplication(ApplicationsBundleEntityApplication $applications)
{
    $this->addAppsCount(1);
    $this->applications[] = $applications;
    return $this;
}
/**
 * Remove applications
 *
 * @param ApplicationsBundleEntityApplication $applications
 */
public function removeApplication(ApplicationsBundleEntityApplication $applications)
{
    $this->addAppsCount(-1);
    $this->applications->removeElement($applications);
}
/**
 * @param integer $appsCount
 * @return integer
 */
public function addAppsCount($appsCount)
{
    $this->appsCount += $appsCount;
    return $this;
}

我还有表单向我的应用程序实体添加类别像这样:

$builder->add('categories', 'entity', array(
                'multiple' => true,
                'expanded' => true,
                'property' => 'namePl',
                'class'    => 'ApplicationsBundleEntityCategory',
        ));

当我将类别添加到我的应用程序时,计数器不会更改其始终为 0(控制器的默认值)为什么会这样?控制器操作: $em = $this->getEntityManager();

    $applicationsRepo = $em->getRepository('ApplicationsBundle:Application');
    $application = $applicationsRepo->findOneByInName($inName);
    if (!$application) {
        $application = new Application($inName);
        $em->persist($application);
        $em->flush();
    }
    $form = $this->createForm(new ApplicationType(), $application);
    $form->handleRequest($this->getRequest());
    if ($form->isValid()) {
        $em->persist($application);
        $em->flush();
        return $this->redirect($this->generateUrl('admin_applications_application_list'));
    }
    return array(
        'application' => $application,
        'form' => $form->createView(),
    );

在使实体调用方法

的情况下
public function addApplication(ApplicationsBundleEntityApplication $applications)
{
    $this->addAppsCount(1);
    $this->applications[] = $applications;
    return $this;
}

我不得不将参数添加到我的表单生成器中:

$builder->add('categories', 'entity', array(
                'multiple' => true,
                'expanded' => true,
                'property' => 'namePl',
                'class'    => 'ApplicationsBundleEntityCategory',
                'by_reference' => false,
        ));

它是如何工作的? 表单编辑应用程序实体的类别字段通过复选框。它使用getCategories()来显示类别,当我们检查类别时,它会创建关系 beetwen 它们运行方法:

public function addCategory(ApplicationsBundleEntityCategory $categories)
{
    $this->categories[] = $categories;
    $categories->addApplication($this);
    return $this;
}

这一行很重要:

    $categories->addApplication($this);

使用它向类别添加应用程序。

相关内容

  • 没有找到相关文章

最新更新