Я Хава отношения многие-ко-многим с организацией продукта и возможности лица субъекта продукта:Doctrine2 обновление многие-ко-многим отношениям
/**
* @ORM\ManyToMany(targetEntity="Feature")
* @ORM\JoinTable(name="Product_Feature",
* joinColumns={@ORM\JoinColumn(name="Product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="Feature_id", referencedColumnName="id")}
* )
*/
private $features;
Характеристика объекта:
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="features")
* @ORM\OrderBy({"position" = "ASC"})
*/
private $products;
ProductRepository.php:
public function updateFeatures($id, $featuresIds)
{
return $this->getEntityManager()->createQueryBuilder()
->update('TestCatalogBundle:Product', 'p')
->set('p.features', ':features')
->where('p.id = :id')
->setParameter('features', $featuresIds)
->setParameter('id', $id)
->getQuery()
->getResult();
}
Но когда я называю updateFeatures я получаю сообщение об ошибке:
features = :features': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected
Как обновить таблицу Product_Feature? Также я не могу удалить все функции из Product_Feature по идентификатору продукта.
Я изменил мой контроллер в следующем образом:
$em = $this->getDoctrine()->getEntityManager();
$features = $em->getRepository('TestCatalogBundle:Feature')->findBy(array('id' => $featureIds));
$product = $em->getRepository('TestCatalogBundle:Product')->find($id);
$product->getFeatures()->clear();
foreach ($features as $feature) {
$product->addFeature($feature);
}
$em->persist($product);
$em->flush();
Но если я использую родной SQL мне нужно 2 запросов для удаления функции и вставлять новые функции. Но здесь мне нужно 2 запроса. Может быть, я сделал это неправильно?
** Ответ **: Пожалуйста, обратитесь к ответу я сделал на HTTP: // stackoverflow.com/a/35445060/2423563 – SudarP