无法保存(通过关系找到了新实体)

时间:2016-10-10 07:51:43

标签: doctrine symfony

错误讯息:

  

"通过这种关系找到了一个新的实体   '的appbundle \实体\目录#产品'没有配置   实体的级联持久化操作:   的appbundle \实体\产品@ 00000000133d712400000000104ed306。要解决   此问题:在此显式调用EntityManager#persist()   未知实体或配置级联持久化此关联   映射例如@ManyToOne(..,cascade = {" persist"})。如果你不能   找出导致问题实施的实体   '的appbundle \实体\产品#__的toString()'得到一个线索。"

/的appbundle /实体/产品

class Product
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=100)
 */
private $name;

/**
 * @ORM\Column(type="decimal", scale=2)
 */
private $price;

/**
 * @ORM\Column(type="text")
 */
private $description;

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
private $category;

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Product
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Set price
 *
 * @param string $price
 *
 * @return Product
 */
public function setPrice($price)
{
    $this->price = $price;

    return $this;
}

/**
 * Get price
 *
 * @return string
 */
public function getPrice()
{
    return $this->price;
}

/**
 * Set description
 *
 * @param string $description
 *
 * @return Product
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get description
 *
 * @return string
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set category
 *
 * @param \AppBundle\Entity\Category $category
 *
 * @return Product
 */
public function setCategory(\AppBundle\Entity\Category $category = null)
{
    $this->category = $category;

    return $this;
}

/**
 * Get category
 *
 * @return \AppBundle\Entity\Category
 */
public function getCategory()
{
    return $this->category;
}
}

/的appbundle /实体/分类

class Category
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
protected $name;

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

public function __construct()
{
    $this->products = new ArrayCollection();
}


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Category
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Add product
 *
 * @param \AppBundle\Entity\Product $product
 *
 * @return Category
 */
public function addProduct(\AppBundle\Entity\Product $product)
{
    $this->products[] = $product;

    return $this;
}

/**
 * Remove product
 *
 * @param \AppBundle\Entity\Product $product
 */
public function removeProduct(\AppBundle\Entity\Product $product)
{
    $this->products->removeElement($product);
}

/**
 * Get products
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getProducts()
{
    return $this->products;
}
}

/的appbundle /窗体/ CategoryType

class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name');
    $builder->add('products', CollectionType::class, array(
        'entry_type' => ProductType::class
    ));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
       'data_class' => 'AppBundle\Entity\Category'
    ));
}

public function getName()
{
    return 'category';
}
}

/的appbundle /窗体/ ProductType

class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name')
            ->add('price')
            ->add('description');

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
       'data_class' => 'AppBundle\Entity\Product',
    ));
}

public function getName()
{
    return 'app_bundle_product_type';
}
}

/的appbundle /控制器/ CategoryController

class CategoryController extends Controller
{
/**
 * @Route("/category/new", name="category_new")
 *
 */
public function newAction(Request $request)
{
    $category = new Category();

    $product = new Product();
    $category->getProducts()->add($product);

    $form = $this->createForm(CategoryType::class, $category);

    $form->handleRequest($request);

    if ($form->isValid() && $form->isSubmitted()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($category);
        $em->flush();
    }

    return $this->render('category/new.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

/category/new.html.twig

{% extends 'base.html.twig' %}

{% block body %}

{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary">Save</button>
{{ form_end(form) }}

{% endblock %}

1 个答案:

答案 0 :(得分:2)

您应该更改关联,以告诉学说保留所有新类别产品

...
/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category", cascade={"persist", "remove"})
 */
protected $products;
...

或手动持续

...
$em->persist($category);
$em->persist($product);
$em->flush();
...

还设置产品类别

...
$product = new Product();
$product->setCategory($category);
...