【发布时间】:2015-12-01 18:17:23
【问题描述】:
帮助,
在控制器中我通常喜欢$this->getDoctrine()->getManager()->getRepository('ArtelProfileBundle:Teams')
->find($id)
但是有 Proxies_CG_\ProfileBundle\Entity\Teams 为什么?因为这是一种 PUT 方法,我用于新对象和旧对象,但新对象几乎是实体 Artel\ProfileBundle\Entity\Teams 并且与 Proxies_CG_\Artel\ProfileBundle\Entity\Teams 不同
示例 teamOld = Proxies_CG_\ProfileBundle\Entity\Teams teamNew = Artel\ProfileBundle\Entity\Teams
我试试
$qb = $this->getEntityManager()->createQueryBuilder('d');
$qb
->select('d')
->from('ArtelProfileBundle:Teams', 'd')
->andWhere('d.id = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
但仍然是 Proxies_CG_\Artel\ProfileBundle\Entity\Teams 我试试
$proxyObject->__load();
但只添加信息,但我需要为我的服务 updateObject 成为 Artel\ProfileBundle\Entity\Teams 的实体
public function putTeamAction(Request $request, $id)
{
$teamOld = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->putTeamClient($id);
if (!empty($user) && !empty($token)) {
$data = $this->get('serializer')->serialize($data, 'json');
$teamOld = $this->getDoctrine()->getManager()->getRepository('ArtelProfileBundle:Teams')
->find($id);
$teamNew = $this->get('serializer')
->deserialize($data, 'Artel\ProfileBundle\Entity\Teams', 'json');
$this->get('artel.project.update')->updateObject($teamOld, $teamNew);
$this->getDoctrine()->getManager()->flush();
return "Team successful update";
}
我不明白我对另一个实体项目有确切的 PUT 操作。 为什么学说为同一实体返回另一个实体?对于团队-> 代理、项目-> 完全实体项目?在这个动作中我没有这个问题 团队(代理)
<?php
namespace Artel\ProfileBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use FOS\ElasticaBundle\Configuration\Search;
/**
* Teams
*
* @ORM\Table(name="teams")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\TeamsRepository")
* @Search(repositoryClass="Artel\ProfileBundle\Entity\Repository\ArticleRepository")
*/
class Teams
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
项目(完全实体)
<?php
namespace Artel\ProfileBundle\Entity;
use Doctrine\ORM\Mapping as ORM,
Gedmo\Mapping\Annotation as Gedmo,
Symfony\Component\Validator\Constraints as Assert;
use Artel\ProfileBundle\Helper\HelperMethod;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Type;
/**
* Project
*
* @ORM\Table(name="project")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository ProjectRepository")
* @ExclusionPolicy("all")
*/
class Project
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @Expose()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
o_0 有什么不同?
public function updateObject($objectOld, $objectNew)
{
if (get_class($objectOld) != get_class($objectNew)) {
throw new \Exception('class not equals');
}
$accessor = new PropertyAccessor();
$reflect = new \ReflectionClass($objectOld);
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
$propertyName = $property->getName();
$newValue = $accessor->getValue($objectNew, $propertyName);
if ($newValue !== null) {
$accessor->setValue($objectOld, $propertyName, $newValue);
}
}
return $objectOld;
}
更新魔法 当我从参数中删除 $id 并添加参数时,在开始操作中我 findOneById 我已经完成了实体。但是当我在开始操作中删除 findOneById 再次有代理。当我使用 $sampleOld findOneById 时,我仍然有代理。仅在开始操作时 $team = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->findOneById($team_id);我已经完成并且在 $sampleOld = $manager->getRepository('ArtelProfileBundle:Teams') 之后 ->查找($team_id);已完成实体。这是一个魔法。
public function putTeamAction(Request $request)
{
$team_id = $this->get('request')->request->get('id');
$team = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->findOneById($team_id);
$manager = $this->getDoctrine()->getManager();
$token = $this->get('request')->request->get('token');
$user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
$data = $request->request->all();
$view = View::create();
if (!empty($user) && !empty($token)) {
$data = $this->get('serializer')->serialize($data, 'json');
$sampleOld = $manager->getRepository('ArtelProfileBundle:Teams')
->find($team_id);
$sampleNew = $this->get('serializer')
->deserialize($data, 'Artel\ProfileBundle\Entity\Teams', 'json');
if (!$sampleOld) {
$manager->persist($sampleNew);
$manager->flush();
$view = $this->view('Project successful create', 200);
return $this->handleView($view);
} else {
$this->get('artel.project.update')->updateObject($sampleOld, $sampleNew);
$manager->flush();
$view = $this->view('Project successful update', 200);
return $this->handleView($view);
}
}else{
$view = $this->view('Secret token is not valid', 101);
return $this->handleView($view);
}
}
【问题讨论】:
标签: php symfony doctrine-orm