dongtao5104 2015-12-01 18:17
浏览 61

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

Help, In controller I like usually $this->getDoctrine()->getManager()->getRepository('ArtelProfileBundle:Teams') ->find($id)

but have Proxies_CG_\ProfileBundle\Entity\Teams why?? Because this is a PUT method and I use for new and old object but new object this is almost entity Artel\ProfileBundle\Entity\Teams and have different with Proxies_CG_\Artel\ProfileBundle\Entity\Teams

example teamOld = Proxies_CG_\ProfileBundle\Entity\Teams teamNew = Artel\ProfileBundle\Entity\Teams

I try

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

but still Proxies_CG_\Artel\ProfileBundle\Entity\Teams I try

$proxyObject->__load();

but only add information, but I need entity who to be 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";
}

and I don’t understand I have exactly PUT action for another entity project. why doctrine return another entity for same entity? for team -> proxies, project -> completely entity project? and in this action I don’t have this problem team(proxies)

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

project(completely entity)

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

What is different 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 when I deleted $id from param and add in parameters and in start action I findOneById I have completed entity. But when I deleted in start action findOneById again have proxies. And when I use for $sampleOld findOneById I have still proxies. Only when in start action $team = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->findOneById($team_id); I have completed and after $sampleOld = $manager->getRepository('ArtelProfileBundle:Teams') ->find($team_id); have completed entity. This a magic.

   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条回答 默认 最新

  • douyi8760 2015-12-02 08:18
    关注

    but have Proxies_CG_\ProfileBundle\Entity\Teams why??

    This is normal doctrine behavior. You should change your updateObject method. I know it is universal, but has issues, when you try to update subclasses. Look at Memento pattern or implement update in entity class $entity->updateFrom(Entity $entity);

    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加