Symfony Doctrine,find()返回代理实体

时间:2015-12-01 18:17:23

标签: php symfony doctrine-orm

帮助, 在控制器中,我通常喜欢$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();

但只添加信息,但我需要实体谁是Artel \ ProfileBundle \ Entity \ Teams for my service updateObject

    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;
}

UPDATE MAGIC 当我从param中删除$ id并添加参数并在启动操作中我findOneById我已完成实体。但是当我在开始动作中删除时,findOneById再次拥有代理。当我使用$ sampleOld findOneById时,我还有代理。仅在开始操作时$ team = $ this-&gt; getDoctrine() - &gt; getRepository(&#39; ArtelProfileBundle:Teams&#39;) - &gt; findOneById($ team_id);我已经完成了$ sampleOld = $ manager-&gt; getRepository(&#39; ArtelProfileBundle:Teams&#39;)                  - &GT;找到($ 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);
    }
}

1 个答案:

答案 0 :(得分:1)

  

Proxies_CG_\ProfileBundle\Entity\Teams为什么?

这是正常的学说行为。您应该更改updateObject方法。我知道它是通用的,但是当你尝试更新子类时会有问题。在实体类update

中查看Memento pattern或实施$entity->updateFrom(Entity $entity);